remove arg len check since we already do it above

This commit is contained in:
Sam Woodard 2015-07-10 08:20:49 -07:00
parent a228611aa0
commit 84eed2060a
2 changed files with 49 additions and 68 deletions

View File

@ -55,27 +55,22 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
return false return false
} }
if len(args) >= 2 { if data.Type != args[1] {
if data.Type != args[1] { er = errors.New("Trying to Unmarshal a type that does not match")
er = errors.New("Trying to Unmarshal a type that does not match") return false
return false }
}
if fieldValue.Kind() == reflect.String { if fieldValue.Kind() == reflect.String {
fieldValue.Set(reflect.ValueOf(data.Id)) fieldValue.Set(reflect.ValueOf(data.Id))
} else if fieldValue.Kind() == reflect.Int { } else if fieldValue.Kind() == reflect.Int {
id, err := strconv.Atoi(data.Id) id, err := strconv.Atoi(data.Id)
if err != nil { if err != nil {
er = err er = err
return false
}
fieldValue.SetInt(int64(id))
} else {
er = errors.New("Unsuppored data type for primary key, not int or string")
return false return false
} }
fieldValue.SetInt(int64(id))
} else { } else {
er = errors.New("'type' as second argument required for 'primary'") er = errors.New("Unsuppored data type for primary key, not int or string")
return false return false
} }
} else if annotation == "attr" { } else if annotation == "attr" {
@ -84,43 +79,39 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
return false return false
} }
if len(args) >= 2 { val := attributes[args[1]]
val := attributes[args[1]]
// next if the attribute was not included in the request // next if the attribute was not included in the request
if val == nil { if val == nil {
return false return false
} }
v := reflect.ValueOf(val) v := reflect.ValueOf(val)
if fieldValue.Type() == reflect.TypeOf(time.Time{}) { if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
var at int64 var at int64
if v.Kind() == reflect.Float64 { if v.Kind() == reflect.Float64 {
at = int64(v.Interface().(float64)) at = int64(v.Interface().(float64))
} else if v.Kind() == reflect.Int { } else if v.Kind() == reflect.Int {
at = v.Int() at = v.Int()
} else {
er = errors.New("Only numbers can be parsed as dates, unix timestamps")
return false
}
t := time.Unix(at, 0)
fieldValue.Set(reflect.ValueOf(t))
return false
}
if fieldValue.Kind() == reflect.Int && v.Kind() == reflect.Float64 {
fieldValue.Set(reflect.ValueOf(int(v.Interface().(float64))))
} else { } else {
fieldValue.Set(reflect.ValueOf(val)) er = errors.New("Only numbers can be parsed as dates, unix timestamps")
return false
} }
t := time.Unix(at, 0)
fieldValue.Set(reflect.ValueOf(t))
return false
}
if fieldValue.Kind() == reflect.Int && v.Kind() == reflect.Float64 {
fieldValue.Set(reflect.ValueOf(int(v.Interface().(float64))))
} else { } else {
er = errors.New("Attribute key required as second arg") fieldValue.Set(reflect.ValueOf(val))
} }
} else if annotation == "relation" { } else if annotation == "relation" {
isSlice := fieldValue.Type().Kind() == reflect.Slice isSlice := fieldValue.Type().Kind() == reflect.Slice

View File

@ -109,35 +109,25 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
annotation := args[0] annotation := args[0]
if annotation == "primary" { if annotation == "primary" {
if len(args) >= 2 { node.Id = fmt.Sprintf("%v", fieldValue.Interface())
node.Id = fmt.Sprintf("%v", fieldValue.Interface()) node.Type = args[1]
node.Type = args[1]
} else {
er = errors.New("'type' as second argument required for 'primary'")
return false
}
} else if annotation == "attr" { } else if annotation == "attr" {
if node.Attributes == nil { if node.Attributes == nil {
node.Attributes = make(map[string]interface{}) node.Attributes = make(map[string]interface{})
} }
if len(args) >= 2 { if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
if fieldValue.Type() == reflect.TypeOf(time.Time{}) { isZeroMethod := fieldValue.MethodByName("IsZero")
isZeroMethod := fieldValue.MethodByName("IsZero") isZero := isZeroMethod.Call(make([]reflect.Value, 0))[0].Interface().(bool)
isZero := isZeroMethod.Call(make([]reflect.Value, 0))[0].Interface().(bool) if isZero {
if isZero { return false
return false
}
unix := fieldValue.MethodByName("Unix")
val := unix.Call(make([]reflect.Value, 0))[0]
node.Attributes[args[1]] = val.Int()
} else {
node.Attributes[args[1]] = fieldValue.Interface()
} }
unix := fieldValue.MethodByName("Unix")
val := unix.Call(make([]reflect.Value, 0))[0]
node.Attributes[args[1]] = val.Int()
} else { } else {
er = errors.New("'type' as second argument required for 'primary'") node.Attributes[args[1]] = fieldValue.Interface()
return false
} }
} else if annotation == "relation" { } else if annotation == "relation" {