MCPcopy Create free account
hub / github.com/pace/bricks / visitModelNode

Function visitModelNode

http/jsonapi/response.go:199–520  ·  view source on GitHub ↗
(model interface{}, included *map[string]*Node,
	sideload bool,
)

Source from the content-addressed store, hash-verified

197}
198
199func visitModelNode(model interface{}, included *map[string]*Node,
200 sideload bool,
201) (*Node, error) {
202 node := new(Node)
203
204 var er error
205 value := reflect.ValueOf(model)
206 if value.IsNil() {
207 return nil, nil
208 }
209
210 modelValue := value.Elem()
211 modelType := value.Type().Elem()
212
213 for i := 0; i < modelValue.NumField(); i++ {
214 structField := modelValue.Type().Field(i)
215 tag := structField.Tag.Get(annotationJSONAPI)
216 if tag == "" {
217 continue
218 }
219
220 fieldValue := modelValue.Field(i)
221 fieldType := modelType.Field(i)
222
223 args := strings.Split(tag, annotationSeperator)
224
225 if len(args) < 1 {
226 er = ErrBadJSONAPIStructTag
227 break
228 }
229
230 annotation := args[0]
231
232 if (annotation == annotationClientID && len(args) != 1) ||
233 (annotation != annotationClientID && len(args) < 2) {
234 er = ErrBadJSONAPIStructTag
235 break
236 }
237
238 if annotation == annotationPrimary {
239 v := fieldValue
240
241 // Deal with PTRS
242 var kind reflect.Kind
243 if fieldValue.Kind() == reflect.Ptr {
244 kind = fieldType.Type.Elem().Kind()
245 v = reflect.Indirect(fieldValue)
246 } else {
247 kind = fieldType.Type.Kind()
248 }
249
250 // Handle allowed types
251 switch kind {
252 case reflect.String:
253 node.ID = v.Interface().(string)
254 case reflect.Int:
255 node.ID = strconv.FormatInt(int64(v.Interface().(int)), 10)
256 case reflect.Int8:

Callers 4

marshalOneFunction · 0.85
marshalManyFunction · 0.85

Calls 14

appendIncludedFunction · 0.85
toShallowNodeFunction · 0.85
TypeMethod · 0.80
ErrorfMethod · 0.80
SetMethod · 0.80
LenMethod · 0.80
validateMethod · 0.80
GetMethod · 0.65
JSONAPILinksMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…