Skip to content

Commit

Permalink
Merge pull request #4 from intelligentpos/feature/get-name-from-tags-…
Browse files Browse the repository at this point in the history
…with-prefix

add NamesFromTagWithPrefix method
  • Loading branch information
geototti21 authored Jul 21, 2017
2 parents 27e6dca + 9dfce21 commit c9a6e52
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ go get github.com/intelligentpos/structextract
Field4: 123,
}

// Create a new extractor,we have to pass a pointer to a struct
//Create a new extractor,we have to pass a pointer to a struct
extract := New(&ss)


Expand All @@ -40,19 +40,25 @@ go get github.com/intelligentpos/structextract
values, _ := extract.Values()


//Names will return an array of the field names, []string
//Names will return a slice of the field names, []string
//["Field1","Field2","Field3","Field4"]
names, _ := extract.Names()


//NamesFromTag will return an array of the tag value for the given tag, map[string]interface{}
//NamesFromTag will return a slice of the tag value for the given tag, []string
//["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")
//ValuesFromTag will return a slice of the values of the fields with the give tag
//["value 1", "value 2", true]
valuesFromTag, _ := extract.ValuesFromTag("db")


//NamesFromTagWithPrefix will return a slice of the tag value for the given tag
//including the provided prefix, []string
//["default_field_1_db","default_field_2_db","default_field_3_db"]
tagwithprefix,_ := extract.NamesFromTagWithPrefix("db","default_")


//FieldValueMap will return a map of field name to value, map[string]interface{}
Expand Down
24 changes: 24 additions & 0 deletions extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package structextract
import (
"errors"
"reflect"
"strings"
)

// Extractor holds the struct that we want to extract data from
Expand Down Expand Up @@ -59,6 +60,29 @@ func (e *Extractor) NamesFromTag(tag string) (out []string, err error) {
return
}

//NamesFromTagWithPrefix returns an array with all the tag names for each field including the given prefix
func (e *Extractor) NamesFromTagWithPrefix(tag string, prefix string) (out []string, 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
}
val, ok := s.Type().Field(i).Tag.Lookup(tag)
if !ok {
continue
}
out = append(out, strings.TrimSpace(prefix+val))
}

return
}

//Values returns an interface array with all the values
func (e *Extractor) Values() (out []interface{}, err error) {

Expand Down
66 changes: 66 additions & 0 deletions extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package structextract

import (
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -86,6 +87,59 @@ func TestExtractor_NamesFromTag_Invalid_Struct(t *testing.T) {

}

func TestExtractor_NamesFromTagWithPrefix(t *testing.T) {
ext := fakeData()
prefix := "default_"
res, err := ext.NamesFromTagWithPrefix("json", prefix)
if err != nil {
t.Fatalf("unexpected error")
}

if !strings.Contains(res[1], prefix) {
t.Fatalf("prefix was not applied")
}

}

func TestExtractor_NamesFromTagWithPrefix_Empty_Tag(t *testing.T) {

ext := fakeData()
prefix := "default_"
out, err := ext.NamesFromTagWithPrefix("", prefix)
if err != nil {
t.Fatalf("error should be null")
}
if len(out) != 0 {
t.Fatalf("no objects was expected")
}

}
func TestExtractor_NamesFromTagWithPrefix_No_Prefix(t *testing.T) {
ext := fakeData()
resWith, err := ext.NamesFromTagWithPrefix("json", "")
if err != nil {
t.Fatalf("unexpected error")
}
resWithOut, err := ext.NamesFromTag("json")
if err != nil {
t.Fatalf("unexpected error")
}

if !reflect.DeepEqual(resWith, resWithOut) {
t.Fatalf("slices micmatch")
}

}

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

_, err := ext.NamesFromTagWithPrefix("json", "default-")
if err == nil {
t.Fatal("Passed value is not a valid struct")
}
}
func TestExtractor_Values(t *testing.T) {
ext := fakeData()
exp := []interface{}{
Expand Down Expand Up @@ -243,6 +297,18 @@ func TestExtractor_GetFieldNamesFromTagIgnore(t *testing.T) {
t.FailNow()
}
}
func TestExtractor_GetFieldNamesFromTagWithPrefixIgnore(t *testing.T) {
ext := fakeIgnoredData()
exp := []string{
"default_field_1",
"default_field_3",
}
res, _ := ext.NamesFromTagWithPrefix("json", "default_")

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

func TestExtractor_ValuesIgnore(t *testing.T) {
ext := fakeIgnoredData()
Expand Down

0 comments on commit c9a6e52

Please sign in to comment.