Skip to content

Commit

Permalink
Solution for #13
Browse files Browse the repository at this point in the history
  • Loading branch information
guiferpa committed Nov 28, 2020
1 parent 57a2a40 commit 1fd4896
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ steps:
image: golang:1.13
commands:
- go test -v -bench=. ./...

- name: coverage
image: golang:1.13
commands:
- go get github.com/mattn/goveralls
- goveralls -v -service drone.io
- goveralls -v -service drone.io
environment:
COVERALLS_TOKEN:
from_secret: COVERALLS_TOKEN
2 changes: 2 additions & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/guiferpa/gody/v2

go 1.13

require github.com/guiferpa/gody v1.1.0
2 changes: 2 additions & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/guiferpa/gody v1.1.0 h1:Z27J1+7zEHt3JRpa09MemaOIiJlpxqOCEMdo8r+y5Vc=
github.com/guiferpa/gody v1.1.0/go.mod h1:YTFw2BBAids7PWNjJ9L45y4+QJAx0jl2W0q58B1YndI=
3 changes: 3 additions & 0 deletions v2/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func RawSerialize(tn string, b interface{}) ([]Field, error) {

fieldValue := valueOf.FieldByName(field.Name)
fieldNameToLower := strings.ToLower(field.Name)
if fieldNameFromJSONTag := field.Tag.Get("json"); fieldNameFromJSONTag != "" {
fieldNameToLower = fieldNameFromJSONTag
}
if kindOfField := field.Type.Kind(); kindOfField == reflect.Struct {
if fieldConverted := fieldValue.Convert(fieldValue.Type()); fieldConverted.CanInterface() {
payload := fieldConverted.Interface()
Expand Down
23 changes: 23 additions & 0 deletions v2/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,29 @@ func TestRawSerializeWithEmptyTagName(t *testing.T) {
}
}

type TestStructJSON struct {
A string `validate:"not_empty" json:"b"`
}

func TestRawSerializeWithJSONTagName(t *testing.T) {
body := struct {
A string `json:"b" validate:"not_empty"`
}{}

fields, err := RawSerialize("validate", body)
if err != nil {
t.Error(err)
return
}

field := fields[0]

if got, want := field.Name, "b"; got != want {
t.Errorf("Unexpected field name, got: %s, want: %s", got, want)
return
}
}

func BenchmarkSerializeBodyStruct(b *testing.B) {
b.ResetTimer()
body := map[string]string{"test-key": "test-value"}
Expand Down

0 comments on commit 1fd4896

Please sign in to comment.