(model interface{}, included *map[string]*Node,
sideload bool,
)
| 197 | } |
| 198 | |
| 199 | func 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: |
no test coverage detected
searching dependent graphs…