Skip to content

Commit

Permalink
Merge pull request #1 from intelligentpos/develop
Browse files Browse the repository at this point in the history
Merge Develop to Master
  • Loading branch information
geototti21 authored Apr 3, 2017
2 parents 32f5266 + 8af62eb commit 0e53e2a
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 2 deletions.
119 changes: 118 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,119 @@
# structextract
Take things out of a struct
A very small package that extracts a given struct to an array or to a map.
There is option to ignore fields or to use the tag names as key on the struct.

## Install

```bash
go get github.com/intelligentpos/structextract
```

## Examples

#### Basic Usage
```go
type SampleStruct struct {
Field1 string `json:"field1" db:"field_1_db"`
Field2 string `json:"field2" db:"field_2_db"`
Field3 bool `json:"field3" db:"field_3_db"`
Field4 int `json:"field4"`
}


ss := SampleStruct{
Field1: "value 1",
Field2: "value 2",
Field3: true,
Field4: 123,
}


extract := New(&ss)


//Values will return the values of every field, []interface
//["value 1", "value 2", true, 123]
values, _ := extract.Values()


//Names will return an array 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{}
//["field_1_db","field_2_db","field_3_db"]
tagnames, _ := extract.NamesFromTag("db")


//FieldValueMap will return a map of field name to value, map[string]interface{}
//{"Field1":"value 1","Field2":"value 2","Field3":true,"Field4":123}
valuesmap, _ := extract.FieldValueMap()


//FieldValueFromTagMap will return a map of tag value to value, map[string]interface{}
//{"field1":"value 1","field2":"value 2","field3":true,"field4":123}
tagmap, _ := extract.FieldValueFromTagMap("json")

```
#### Ignore Fields
```go
ss := SampleStruct{
Field1: "value 1",
Field2: "value 2",
Field3: true,
Field4: 123,
}


// Set all the struct fields that we need to ignore
extract := New(&ss).
IgnoreField("Field2").
IgnoreField("Field3")


//The result will be {"Field1":"value 1","Field4":123},
//all the fields that are ignored are not present.
valuesmap, _ := extract.FieldValueMap()

```

#### Use cases

We found that is very convenient to use structextract when we want to create sql statements
or maps with data to update or create.

A sample example that we use the structextract with [Squirrel](https://github.com/Masterminds/squirrel).

```go
type SampleStruct struct {
Field1 string `json:"field1" db:"field_1_db"`
Field2 string `json:"field2" db:"field_2_db"`
Field3 bool `json:"field3" db:"field_3_db"`
Field4 int `json:"field4"`
}


ss := SampleStruct{
Field1: "value 1",
Field2: "value 2",
Field3: true,
Field4: 123,
}

//Create a map with all the fields a user can update
ext := structextract.New(&ss).
IgnoreField("Field2")


bm, _ := ext.FieldValueFromTagMap("db")

//Build the query
query, args, _ := squirrel.Update("DBTable").
SetMap(bm).
Where(squirrel.Eq{"id": ss.Field1}).
ToSql()

//Make the sql request..
```

2 changes: 1 addition & 1 deletion extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Extractor holds the struct that we wont to extract data from
type Extractor struct {
StructAddr interface{} // StructAddr: struct address
ignoredFields []string //ignoredFields: an array with all the fields to be ignored
ignoredFields []string // ignoredFields: an array with all the fields to be ignored
}

// New returns a new Extractor struct
Expand Down
55 changes: 55 additions & 0 deletions extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func TestExtractor_Names(t *testing.T) {

}

func TestExtractor_Names_Invalid_Struct(t *testing.T) {
test := "test"
ext := New(&test)

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

}
func TestExtractor_NamesFromTag(t *testing.T) {
ext := fakeData()
exp := []string{
Expand All @@ -66,6 +76,17 @@ func TestExtractor_NamesFromTag(t *testing.T) {
}
}

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

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

}

func TestExtractor_Values(t *testing.T) {
ext := fakeData()
exp := []interface{}{
Expand All @@ -81,6 +102,17 @@ func TestExtractor_Values(t *testing.T) {
}
}

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

_, err := ext.Values()
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 All @@ -95,6 +127,18 @@ func TestExtractor_FieldValueMap(t *testing.T) {
t.FailNow()
}
}

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

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

}

func TestExtractor_FieldValueFromTagMap(t *testing.T) {
ext := fakeData()
exp := map[string]interface{}{
Expand All @@ -111,6 +155,17 @@ func TestExtractor_FieldValueFromTagMap(t *testing.T) {

}

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

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

}

func TestExtractor_GetFieldNamesIgnore(t *testing.T) {
ext := fakeIgnoredData()
exp := []string{
Expand Down

0 comments on commit 0e53e2a

Please sign in to comment.