Skip to content

Commit

Permalink
Merge pull request #5 from intelligentpos/feature/get-values-from-tag…
Browse files Browse the repository at this point in the history
…-support

Add support for extracting values of specific tagged struct fields
  • Loading branch information
geototti21 authored Jul 20, 2017
2 parents 58a9e79 + 833f10d commit 27e6dca
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ go get github.com/intelligentpos/structextract
//NamesFromTag will return an array of the tag value for the given tag, map[string]interface{}
//["field_1_db","field_2_db","field_3_db"]
tagnames, _ := extract.NamesFromTag("db")



//ValuesFromTag will return an array of the values of the fields with the give tag
//["value 1", "value 2", true]
valuesFromTag, _ := extract.ValuesFromTag("db")


//FieldValueMap will return a map of field name to value, map[string]interface{}
//{"Field1":"value 1","Field2":"value 2","Field3":true,"Field4":123}
Expand Down
21 changes: 21 additions & 0 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ func (e *Extractor) Values() (out []interface{}, err error) {
return
}

//ValuesFromTag returns an interface array with all the values of fields with the given tag
func (e *Extractor) ValuesFromTag(tag string) (out []interface{}, err error) {

if err := e.isValidStruct(); err != nil {
return nil, err
}

s := reflect.ValueOf(e.StructAddr).Elem()
for i := 0; i < s.NumField(); i++ {
if isIgnored(s.Type().Field(i).Name, e.ignoredFields) {
continue
}
if _, ok := s.Type().Field(i).Tag.Lookup(tag); ok {
out = append(out, s.Field(i).Interface())
}

}

return
}

// FieldValueMap returns a string to interface map,
// key: field as defined on the struct
// value: the value of the field
Expand Down
32 changes: 30 additions & 2 deletions extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type testStruct struct {
Field1 string `json:"field_1"`
Field2 string `json:"field_2"`
Field1 string `json:"field_1" db:"field1"`
Field2 string `json:"field_2" db:"field2"`
Field3 bool `json:"field_3"`
Field4 interface{} `json:"field_4"`
}
Expand Down Expand Up @@ -112,6 +112,34 @@ func TestExtractor_Values_Invalid_Struct(t *testing.T) {

}

func TestExtractor_ValuesFromTag(t *testing.T) {
ext := fakeData().IgnoreField("Field4")
exp := []interface{}{
"hello",
"world",
}
res, _ := ext.ValuesFromTag("db")

expectedLength := len(exp)
if len(res) != expectedLength {
t.Fatalf("Number of values do not match: expected:%d, got:%d", expectedLength, len(res))
}

if !reflect.DeepEqual(res, exp) {
t.FailNow()
}
}

func TestExtractor_ValuesFromTag_Invalid_Struct(t *testing.T) {
test := []string{"fail", "fail2"}
ext := New(&test)

_, err := ext.ValuesFromTag("json")
if err == nil {
t.Fatal("Passed value is not a valid struct")
}
}

func TestExtractor_FieldValueMap(t *testing.T) {
ext := fakeData()
exp := map[string]interface{}{
Expand Down

0 comments on commit 27e6dca

Please sign in to comment.