MCPcopy Index your code
hub / github.com/53AI/53AIHub / tryEncodeID

Function tryEncodeID

api/middleware/response_encoder.go:121–159  ·  view source on GitHub ↗

tryEncodeID 尝试编码ID值

(value interface{})

Source from the content-addressed store, hash-verified

119
120// tryEncodeID 尝试编码ID值
121func tryEncodeID(value interface{}) interface{} {
122 // 处理不同类型的ID值
123 switch v := value.(type) {
124 case int:
125 if v > 0 {
126 if encoded, err := hashids.Encode(int64(v)); err == nil {
127 return encoded
128 }
129 }
130 case int32:
131 if v > 0 {
132 if encoded, err := hashids.Encode(int64(v)); err == nil {
133 return encoded
134 }
135 }
136 case int64:
137 if v > 0 {
138 if encoded, err := hashids.Encode(v); err == nil {
139 return encoded
140 }
141 }
142 case float64:
143 // JSON数字通常被解析为float64
144 if v > 0 && v == float64(int64(v)) {
145 if encoded, err := hashids.Encode(int64(v)); err == nil {
146 return encoded
147 }
148 }
149 case string:
150 // 如果是字符串形式的数字ID,尝试解析并编码
151 if id, err := strconv.ParseInt(v, 10, 64); err == nil && id > 0 {
152 if encoded, err := hashids.Encode(id); err == nil {
153 return encoded
154 }
155 }
156 }
157
158 return nil
159}
160
161// EncodeResponseIDs 手动编码响应数据中的ID字段(用于特殊情况)
162func EncodeResponseIDs(data interface{}) interface{} {

Callers 2

encodeMapFieldsFunction · 0.85
encodeStructFieldsFunction · 0.85

Calls 1

EncodeMethod · 0.80

Tested by

no test coverage detected