Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from muir/filltag
add TagSet and Tag.Fill and an example
- Loading branch information
Showing
2 changed files
with
185 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package reflectutils_test | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/muir/reflectutils" | ||
) | ||
|
||
type ExampleStruct struct { | ||
String string `foo:"something,N=9,square,!jump"` | ||
Bar string `foo:"different,!square,jump,ignore=xyz"` | ||
} | ||
|
||
type TagExtractorType struct { | ||
Name string `pt:"0"` | ||
Square bool `pt:"square"` | ||
Jump bool `pt:"jump"` | ||
Ignore string `pt:"-"` | ||
N int | ||
} | ||
|
||
// Fill is a helper for when you're working with tags. | ||
func ExampleTag_Fill() { | ||
var es ExampleStruct | ||
t := reflect.TypeOf(es) | ||
reflectutils.WalkStructElements(t, func(f reflect.StructField) bool { | ||
var tet TagExtractorType | ||
err := reflectutils.SplitTag(f.Tag).Set().Get("foo").Fill(&tet) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
fmt.Printf("%s: %+v\n", f.Name, tet) | ||
return true | ||
}) | ||
// Output: String: {Name:something Square:true Jump:false Ignore: N:9} | ||
// Bar: {Name:different Square:false Jump:true Ignore: N:0} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters