2
2
3
3
Robust & Easy to use model mapper and utility methods for Go ` struct ` . Typical methods increase productivity and make Go development more fun :smile :
4
4
5
- *** v0.4 released and tagged on May 19, 2016 ***
5
+ *** v0.5 [ released] ( https://github.com/jeevatkm/go-model/releases/latest ) and tagged on Jan 02, 2017 ***
6
6
7
7
go-model tested with Go ` v1.2 ` and above.
8
8
@@ -17,6 +17,7 @@ go-model library provides [handy methods](#supported-methods) to process `struct
17
17
* Get struct field ` reflect.Kind ` by field name
18
18
* Get all the struct field tags (` reflect.StructTag ` ) or selectively by field name
19
19
* Get all ` reflect.StructField ` for given struct instance
20
+ * Get or Set by individual field name on struct
20
21
* Add global no traverse type to the list or use ` notraverse ` option in the struct field
21
22
* Options to name map key, omit empty fields, and instruct not to traverse with struct/map/slice
22
23
* Conversions between mixed non-pointer types
@@ -56,6 +57,8 @@ import (
56
57
* Kind - [ usage] ( #kind-method ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#Kind )
57
58
* Tag - [ usage] ( #tag-method ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#Tag )
58
59
* Tags - [ usage] ( #tags-method ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#Tags )
60
+ * Get - [ usage] ( #get-method ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#Get )
61
+ * Set - [ usage] ( #set-method ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#Set )
59
62
* AddNoTraverseType - [ usage] ( #addnotraversetype--removenotraversetype-methods ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#AddNoTraverseType )
60
63
* RemoveNoTraverseType - [ usage] ( #addnotraversetype--removenotraversetype-methods ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#RemoveNoTraverseType )
61
64
* AddConversion - [ usage] ( #addconversion--removeconversion-methods ) , [ godoc] ( https://godoc.org/github.com/jeevatkm/go-model#AddConversion )
@@ -200,6 +203,33 @@ tags, _ := model.Tags(src)
200
203
fmt.Println (" Tags:" , tags)
201
204
```
202
205
206
+ #### Get Method
207
+ I want to get value by field name on my ` struct ` . Yes, it is easy to get it.
208
+ ``` go
209
+ src := SampleStruct {
210
+ BookCount : 100 ,
211
+ BookCode : " GHT67HH00" ,
212
+ }
213
+
214
+ value , _ := model.Get (src, " BookCode" )
215
+ fmt.Println (" Value:" , value)
216
+
217
+ // Output:
218
+ Value: GHT67HH00
219
+ ```
220
+
221
+ #### Set Method
222
+ I want to set value by field name on my ` struct ` . Yes, it is easy to get it.
223
+ ``` go
224
+ src := SampleStruct {
225
+ BookCount : 100 ,
226
+ BookCode : " GHT67HH00" ,
227
+ }
228
+
229
+ err := model.Set (&src, " BookCount" , 200 )
230
+ fmt.Println (" Error:" , err)
231
+ ```
232
+
203
233
#### AddNoTraverseType & RemoveNoTraverseType Methods
204
234
There are scenarios, where you want the object values but not to traverse/look inside the struct object. Use ` notraverse ` option in the model tag for those fields or Add it ` NoTraverseTypeList ` . Customize it as per your need.
205
235
@@ -214,7 +244,7 @@ model.AddNoTraverseType(time.Location{}, &time.Location{})
214
244
model.RemoveNoTraverseType (time.Location {}, &time.Location {})
215
245
```
216
246
217
- #### AddConversion & RemoveConversion Method
247
+ #### AddConversion & RemoveConversion Methods
218
248
219
249
This example registers a custom conversion from the ` int ` to the ` string ` type.
220
250
``` go
0 commit comments