28e53d90fc
- 统一 OpenAI 兼容 /v1 中继 + 渠道/额度/令牌/流水 - 各领域包:relay 模型网关、model 数据层、controller 管理 API
87 lines
1.8 KiB
Go
87 lines
1.8 KiB
Go
// Package kitutil holds the dependency-free helpers shared by the conversion
|
|
// kit packages (dto, types, relayconvert). It moved out of the host's common
|
|
// package as part of the relaykit extraction; common re-exports these for
|
|
// host code.
|
|
package kitutil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"unsafe"
|
|
)
|
|
|
|
func Unmarshal(data []byte, v any) error {
|
|
return json.Unmarshal(data, v)
|
|
}
|
|
|
|
func UnmarshalJsonStr(data string, v any) error {
|
|
return json.Unmarshal(StringToByteSlice(data), v)
|
|
}
|
|
|
|
func DecodeJson(reader io.Reader, v any) error {
|
|
return json.NewDecoder(reader).Decode(v)
|
|
}
|
|
|
|
func Marshal(v any) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func GetJsonType(data json.RawMessage) string {
|
|
trimmed := bytes.TrimSpace(data)
|
|
if len(trimmed) == 0 {
|
|
return "unknown"
|
|
}
|
|
firstChar := trimmed[0]
|
|
switch firstChar {
|
|
case '{':
|
|
return "object"
|
|
case '[':
|
|
return "array"
|
|
case '"':
|
|
return "string"
|
|
case 't', 'f':
|
|
return "boolean"
|
|
case 'n':
|
|
return "null"
|
|
default:
|
|
return "number"
|
|
}
|
|
}
|
|
|
|
// JsonRawMessageToString returns JSON strings as their decoded value and other JSON values as raw text.
|
|
func JsonRawMessageToString(data json.RawMessage) string {
|
|
trimmed := bytes.TrimSpace(data)
|
|
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("null")) {
|
|
return ""
|
|
}
|
|
if trimmed[0] != '"' {
|
|
return string(trimmed)
|
|
}
|
|
var value string
|
|
if err := Unmarshal(trimmed, &value); err != nil {
|
|
return string(trimmed)
|
|
}
|
|
return value
|
|
}
|
|
|
|
func StringToByteSlice(s string) []byte {
|
|
tmp1 := (*[2]uintptr)(unsafe.Pointer(&s))
|
|
tmp2 := [3]uintptr{tmp1[0], tmp1[1], tmp1[1]}
|
|
return *(*[]byte)(unsafe.Pointer(&tmp2))
|
|
}
|
|
|
|
func Any2Type[T any](data any) (T, error) {
|
|
var zero T
|
|
bytes, err := json.Marshal(data)
|
|
if err != nil {
|
|
return zero, err
|
|
}
|
|
var res T
|
|
err = json.Unmarshal(bytes, &res)
|
|
if err != nil {
|
|
return zero, err
|
|
}
|
|
return res, nil
|
|
}
|