Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with json tag #81

Merged
merged 1 commit into from
Oct 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,24 @@ type Data struct {
}
```

Decoding behavior can be changed per field through field tag -- just like what `encoding/json` does.
The decoding of each struct field can be customized by the format string stored under the "facebook" key or the "json" key in the struct field's tag. The "facebook" key is recommended as it's specifically designed for this package.

Following is a sample shows all possible field tags.

```go
// define a facebook feed object.
type FacebookFeed struct {
Id string `facebook:",required"` // this field must exist in response.
// mind the "," before "required".
Id string `facebook:",required"` // this field must exist in response.
// mind the "," before "required".
Story string
FeedFrom *FacebookFeedFrom `facebook:"from"` // use customized field name "from".
CreatedTime string `facebook:"created_time,required"` // both customized field name and "required" flag.
Omitted string `facebook:"-"` // this field is omitted when decoding.
FeedFrom *FacebookFeedFrom `facebook:"from"` // use customized field name "from".
CreatedTime string `facebook:"created_time,required"` // both customized field name and "required" flag.
Omitted string `facebook:"-"` // this field is omitted when decoding.
}

type FacebookFeedFrom struct {
Name, Id string
Name string `json:"name"` // the "json" key also works as expected.
Id string `facebook:"id" json:"shadowed"` // if both "facebook" and "json" key are set, the "facebook" key is used.
}

// create a feed object direct from graph api result.
Expand Down
10 changes: 5 additions & 5 deletions batch_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
)

type batchResultHeader struct {
Name string `facebook=",required"`
Value string `facebook=",required"`
Name string `facebook:",required"`
Value string `facebook:",required"`
}

type batchResultData struct {
Code int `facebook=",required"`
Headers []batchResultHeader `facebook=",required"`
Body string `facebook=",required"`
Code int `facebook:",required"`
Headers []batchResultHeader `facebook:",required"`
Body string `facebook:",required"`
}

func newBatchResult(res Result) (*BatchResult, error) {
Expand Down
32 changes: 27 additions & 5 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ func getValueField(value reflect.Value, fields []string) reflect.Value {
//
// If a field is missing in the result, Decode keeps it unchanged by default.
//
// Decode can read struct field tag value to change default behavior.
// The decoding of each struct field can be customized by the format string stored
// under the "facebook" key or the "json" key in the struct field's tag.
// The "facebook" key is recommended as it's specifically designed for this package.
//
// Examples:
//
Expand All @@ -294,6 +296,12 @@ func getValueField(value reflect.Value, fields []string) reflect.Value {
//
// // use "name" as field name in response.
// TheName string `facebook:"name"`
//
// // the "json" key also works as expected.
// Key string `json:"my_key"`
//
// // if both "facebook" and "json" key are set, the "facebook" key is used.
// Value string `facebook:"value" json:"shadowed"`
// }
//
// To change default behavior, set a struct tag `facebook:",required"` to fields
Expand Down Expand Up @@ -440,7 +448,7 @@ func (res Result) decode(v reflect.Value, fullName string) error {

var field reflect.Value
var fieldInfo reflect.StructField
var name, fbTag, dot string
var name, dot string
var val interface{}
var ok, required bool
var err error
Expand All @@ -457,10 +465,9 @@ func (res Result) decode(v reflect.Value, fullName string) error {
required = false
field = v.Field(i)
fieldInfo = vType.Field(i)
fbTag = fieldInfo.Tag.Get("facebook")

// parse struct field tag
if fbTag != "" {
// parse struct field tag.
if fbTag := fieldInfo.Tag.Get("facebook"); fbTag != "" {
if fbTag == "-" {
continue
}
Expand All @@ -476,6 +483,21 @@ func (res Result) decode(v reflect.Value, fullName string) error {
required = true
}
}
} else {
// compatible with json tag.
fbTag = fieldInfo.Tag.Get("json")

if fbTag == "-" {
continue
}

index := strings.IndexRune(fbTag, ',')

if index == -1 {
name = fbTag
} else {
name = fbTag[:index]
}
}

// embedded field is "expanded" when decoding.
Expand Down
54 changes: 54 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,57 @@ func TestResultDecodeUnmarshaler(t *testing.T) {
t.Fatalf("input should fail due to Unmarshaler.")
}
}

type MixedTagStruct struct {
Foo int `facebook:"bar" json:"player"`
FirstTest string `facebook:"" json:"first"`
SecondTest float64 `json:"second"`
ThirdTest uint32 `json:"-"`
FourthTest int64 `facebook:",required" json:"fourth"`
}

func TestCompatibleWithJSONUnmarshal(t *testing.T) {
res := Result{
"foo": 1,
"bar": 2,
"player": 3,

"first_test": "first",
"first": "test",

"second_test": 1.2,
"second": 3.4,

"third_test": 5,
"-": 6,

"fourth_test": 7,
"fourth": 8,
}
mts := &MixedTagStruct{}
err := res.Decode(mts)

if err != nil {
t.Fatalf("fail to decode result. [e:%v]", err)
}

if expected := 2; mts.Foo != expected {
t.Fatalf("mts.Foo is incorrect. [expected:%v] [actual:%v]", expected, mts.Foo)
}

if expected := "test"; mts.FirstTest != expected {
t.Fatalf("mts.FirstTest is incorrect. [expected:%v] [actual:%v]", expected, mts.FirstTest)
}

if expected := 3.4; mts.SecondTest != expected {
t.Fatalf("mts.SecondTest is incorrect. [expected:%v] [actual:%v]", expected, mts.SecondTest)
}

if expected := uint32(0); mts.ThirdTest != expected {
t.Fatalf("mts.ThirdTest is incorrect. [expected:%v] [actual:%v]", expected, mts.ThirdTest)
}

if expected := int64(7); mts.FourthTest != expected {
t.Fatalf("mts.FourthTest is incorrect. [expected:%v] [actual:%v]", expected, mts.FourthTest)
}
}