Allow parsing of integer array attributes from JSON#73
Allow parsing of integer array attributes from JSON#73sbelkin88 wants to merge 3 commits intogoogle:masterfrom
Conversation
|
There is a larger problem here than just supporting Example: type Position struct {
Latitude string
Longitude string
}
type City struct {
ID int `jsonapi:"primary,city"`
Location Position `jsonapi:"attr,location"`
}{
"data": {
"type": "city",
"id": "1",
"attributes": {
"name": "Toronto",
"location": {
"lat": "43.6532° N",
"lng": "79.3832° W"
},
... other attributes etc
},
... relations etc
}
} |
|
@aren55555 you are correct, this is just supporting a top-level attribute field that is of // Field was a Pointer type
if fieldValue.Kind() == reflect.Ptr {
var concreteVal reflect.Value
switch cVal := val.(type) {
case string:
concreteVal = reflect.ValueOf(&cVal)
case bool:
concreteVal = reflect.ValueOf(&cVal)
case complex64:
concreteVal = reflect.ValueOf(&cVal)
case complex128:
concreteVal = reflect.ValueOf(&cVal)
case uintptr:
concreteVal = reflect.ValueOf(&cVal)
// this is the case we added for our local use
case interface{}:
n := new(Node)
var ok bool
n.Attributes, ok = val.(map[string]interface{})
if !ok {
er = ErrUnsupportedPtrType
break
}
if err := unmarshalNode(n, fieldValue, included); err != nil {
er = ErrUnsupportedPtrType
break
}
concreteVal = fieldValue
default:
return ErrUnsupportedPtrType
}
if fieldValue.Type() != concreteVal.Type() {
return ErrUnsupportedPtrType
}
fieldValue.Set(concreteVal)
continue
} |
| CurrentPostID int `jsonapi:"attr,current_post_id"` | ||
| CreatedAt time.Time `jsonapi:"attr,created_at"` | ||
| ViewCount int `jsonapi:"attr,view_count"` | ||
| BookmarkedPages []int `jsonapi:"attr,bookmarked_pages"` |
There was a problem hiding this comment.
Did this work for serialization/marshaling? ie did it appear in the json payload as expected?
There was a problem hiding this comment.
Yes, this serializes correctly. I just pushed an update including a test for this.
|
I think adding |
|
There is already support for |
|
We sorta do this already by just making a property like this: At this point anything posted underneath that property, as long as its valid JSON, will end up here. We keep anything important specifically at the top level, and otherwise use related objects. PS: For storage, we just convert this to a byte array. |
|
@genexp Are you doing that with the library as it current exists, or are you working with a fork? |
|
hey @bfitzsimmons as it exists! |
|
@genexp Thanks for the quick response. Hmmm... I thought I was trying what you had posted, but I was actually attempting to Unmarshal into a slice of maps. That's definitely not working. Example:Here's the struct (note the slice of maps): type Payload struct {
Field1 string `jsonapi:"attr,field1"`
Field2 string `jsonapi:"attr,field2"`
Stuff []map[string]interface{} `jsonapi:"attr,stuff"`
}Here's the POST payload. {
"data": {
"type": "stuffs",
"attributes": {
"field1": "77005db9-bceb-4f54-a045-26399ba25db2",
"field2": "2cfe125d-b0a5-493b-82e6-69c3f6a45e9a",
"stuff": [
{
"key": "foo",
"value": "yeehaw",
"type": "string"
},
{
"key": "bar",
"value": "true",
"type": "bool"
},
{
"key": "baz",
"value": "42",
"type": "int"
}
]
}
}
}Should this be working as well? If not, is there a workaround that I should be considering? |
|
I added the following to if fieldValue.Type() == reflect.TypeOf([]map[string]interface{}{}) {
values := make([]map[string]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
values[i] = v.Index(i).Interface().(map[string]interface{})
}
fieldValue.Set(reflect.ValueOf(values))
continue
}Good idea? Bad idea? Should I write an appropriate test and submit it? |
|
Seems good to me! Essentially your allowing the root node being mapped to Be a JSON array instead of a JSON object. I dig it! |
|
Hi, Will this PR be merged? |
|
@sbelkin88 Hi there! This looks like an awesome contribution, though I just noticed there are conflicts to resolve. I wonder if you have a moment, could you try resolving these conflicts? Would love to see this contribution make it into the library for my own sake! 😛 |
No description provided.