├── .gitignore ├── v2 ├── profiler │ ├── .gitignore │ └── main.go ├── structs_test.go └── structs.go ├── .travis.yml ├── errors.go ├── README.md ├── structs.go ├── LICENSE.md └── structs_test.go /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /v2/profiler/.gitignore: -------------------------------------------------------------------------------- 1 | main 2 | cpu.pprof 3 | trace.out -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - 1.8.1 4 | 5 | script: 6 | - go test -v ./... -------------------------------------------------------------------------------- /errors.go: -------------------------------------------------------------------------------- 1 | package structs 2 | 3 | import ( 4 | "errors" 5 | ) 6 | 7 | var ( 8 | errReplaceTypesNotMatching = errors.New("Old and new value types does not match") 9 | ) 10 | -------------------------------------------------------------------------------- /v2/profiler/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | "runtime/pprof" 8 | "runtime/trace" 9 | 10 | "github.com/PumpkinSeed/structs/v2" 11 | ) 12 | 13 | var profileBool bool 14 | var traceBool bool 15 | 16 | func init() { 17 | flag.BoolVar(&profileBool, "profile", false, "Start pprof") 18 | flag.BoolVar(&traceBool, "trace", false, "Start trace") 19 | } 20 | 21 | func main() { 22 | flag.Parse() 23 | 24 | if profileBool { 25 | pprof.StartCPUProfile(os.Stdout) 26 | defer pprof.StopCPUProfile() 27 | } 28 | 29 | if traceBool { 30 | trace.Start(os.Stdout) 31 | defer trace.Stop() 32 | } 33 | 34 | testStruct := struct { 35 | TestInt int 36 | TestInt8 int8 37 | TestInt16 int16 38 | TestInt32 int32 39 | TestInt64 int64 40 | TestString string 41 | TestBool bool 42 | TestFloat32 float32 43 | TestFloat64 float64 44 | }{ 45 | TestInt: 12, 46 | TestInt8: 42, 47 | TestInt16: 55, 48 | TestInt32: 33, 49 | TestInt64: 78, 50 | TestString: "test", 51 | TestBool: false, 52 | TestFloat32: 13.444, 53 | TestFloat64: 16.444, 54 | } 55 | 56 | if !profileBool && !traceBool { 57 | fmt.Println("Start test...") 58 | } 59 | 60 | for n := 0; n < 1000000; n++ { 61 | structs.Contains(testStruct, float64(16.444)) 62 | } 63 | 64 | if !profileBool && !traceBool { 65 | fmt.Println("Stop test...") 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /v2/structs_test.go: -------------------------------------------------------------------------------- 1 | package structs 2 | 3 | import "testing" 4 | 5 | func TestContains(t *testing.T) { 6 | testStruct := struct { 7 | TestInt int 8 | TestInt8 int8 9 | TestInt16 int16 10 | TestInt32 int32 11 | TestInt64 int64 12 | TestString string 13 | TestBool bool 14 | TestFloat32 float32 15 | TestFloat64 float64 16 | TestComplex64 complex64 17 | TestComplex128 complex128 18 | }{ 19 | TestInt: 12, 20 | TestInt8: 42, 21 | TestInt16: 55, 22 | TestInt32: 33, 23 | TestInt64: 78, 24 | TestString: "test", 25 | TestBool: false, 26 | TestFloat32: 13.444, 27 | TestFloat64: 16.444, 28 | TestComplex64: 12333, 29 | TestComplex128: 123444455, 30 | } 31 | 32 | result := Contains(testStruct, float64(16.444)) 33 | if !result { 34 | t.Error("Should be true instead of false") 35 | } 36 | 37 | } 38 | 39 | /* 40 | Benchmark 41 | */ 42 | 43 | func BenchmarkContains(b *testing.B) { 44 | testStruct := struct { 45 | TestInt int 46 | TestInt8 int8 47 | TestInt16 int16 48 | TestInt32 int32 49 | TestInt64 int64 50 | TestString string 51 | TestBool bool 52 | TestFloat32 float32 53 | TestFloat64 float64 54 | }{ 55 | TestInt: 12, 56 | TestInt8: 42, 57 | TestInt16: 55, 58 | TestInt32: 33, 59 | TestInt64: 78, 60 | TestString: "test", 61 | TestBool: false, 62 | TestFloat32: 13.444, 63 | TestFloat64: 16.444, 64 | } 65 | 66 | for n := 0; n < b.N; n++ { 67 | Contains(testStruct, float64(16.444)) 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /v2/structs.go: -------------------------------------------------------------------------------- 1 | package structs 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | ) 7 | 8 | /* 9 | ------------------------------------------- 10 | Try to optimize things with Go concurrency, 11 | but as seen it's worse. Bench: 12 | BenchmarkContains-4 300000 4012 ns/op 13 | ------------------------------------------- 14 | */ 15 | 16 | var ( 17 | float32Type = reflect.TypeOf(float32(0)) 18 | float64Type = reflect.TypeOf(float64(0)) 19 | 20 | stringType = reflect.TypeOf("") 21 | 22 | intType = reflect.TypeOf(int(0)) 23 | int8Type = reflect.TypeOf(int8(0)) 24 | int16Type = reflect.TypeOf(int16(0)) 25 | int32Type = reflect.TypeOf(int32(0)) 26 | int64Type = reflect.TypeOf(int64(0)) 27 | 28 | uintType = reflect.TypeOf(uint(0)) 29 | uint8Type = reflect.TypeOf(uint8(0)) 30 | uint16Type = reflect.TypeOf(uint16(0)) 31 | uint32Type = reflect.TypeOf(uint32(0)) 32 | uint64Type = reflect.TypeOf(uint64(0)) 33 | // add uintptr 34 | 35 | // add complex types 36 | complex64Type = reflect.TypeOf(complex64(0)) 37 | complex128Type = reflect.TypeOf(complex128(0)) 38 | 39 | // add byte array type 40 | 41 | // add rune type 42 | 43 | boolType = reflect.TypeOf(true) 44 | 45 | errorType = reflect.TypeOf(errors.New("")) 46 | ) 47 | 48 | // Contains reports whether value is within s 49 | func Contains(s, value interface{}) bool { 50 | v := reflect.ValueOf(s) 51 | check := make(chan bool, 1) 52 | go containsMain(check, v, value) 53 | for { 54 | select { 55 | case c := <-check: 56 | return c 57 | } 58 | } 59 | } 60 | 61 | /* 62 | Contains helper functions 63 | */ 64 | 65 | func containsMain(c chan bool, v reflect.Value, value interface{}) { 66 | for i := 0; i < v.NumField(); i++ { 67 | field := v.Field(i) 68 | typeOfField := field.Type() 69 | switch typeOfField { 70 | case stringType: 71 | go containsString(c, field.String(), value) 72 | case intType, int8Type, int16Type, int32Type, int64Type: 73 | go containsInt(c, field.Int(), value) 74 | case boolType: 75 | go containsBool(c, field.Bool(), value) 76 | case float32Type: 77 | go containsFloat32(c, field.Interface().(float32), value) 78 | case float64Type: 79 | go containsFloat64(c, field.Interface().(float64), value) 80 | case complex64Type: 81 | go containsComplex64(c, field.Interface().(complex64), value) 82 | case complex128Type: 83 | go containsComplex128(c, field.Interface().(complex128), value) 84 | } 85 | } 86 | } 87 | 88 | func containsString(c chan bool, s string, v interface{}) { 89 | switch v.(type) { 90 | case string: 91 | if v.(string) == s { 92 | c <- true 93 | } 94 | } 95 | return 96 | } 97 | 98 | func containsInt(c chan bool, s int64, v interface{}) { 99 | switch v.(type) { 100 | case int64: 101 | if v.(int64) == s { 102 | c <- true 103 | } 104 | case int32: 105 | if int64(v.(int32)) == s { 106 | c <- true 107 | } 108 | case int16: 109 | if int64(v.(int16)) == s { 110 | c <- true 111 | } 112 | case int8: 113 | if int64(v.(int8)) == s { 114 | c <- true 115 | } 116 | case int: 117 | if int64(v.(int)) == s { 118 | c <- true 119 | } 120 | } 121 | return 122 | } 123 | 124 | func containsBool(c chan bool, s bool, v interface{}) { 125 | switch v.(type) { 126 | case bool: 127 | if v.(bool) == s { 128 | c <- true 129 | } 130 | } 131 | return 132 | } 133 | 134 | func containsFloat32(c chan bool, s float32, v interface{}) { 135 | switch v.(type) { 136 | case float32: 137 | if v.(float32) == s { 138 | c <- true 139 | } 140 | } 141 | return 142 | } 143 | 144 | func containsFloat64(c chan bool, s float64, v interface{}) { 145 | switch v.(type) { 146 | case float64: 147 | if v.(float64) == s { 148 | c <- true 149 | } 150 | } 151 | return 152 | } 153 | 154 | func containsComplex64(c chan bool, s complex64, v interface{}) { 155 | switch v.(type) { 156 | case complex64: 157 | if v.(complex64) == s { 158 | c <- true 159 | } 160 | } 161 | return 162 | } 163 | 164 | func containsComplex128(c chan bool, s complex128, v interface{}) { 165 | switch v.(type) { 166 | case complex128: 167 | if v.(complex128) == s { 168 | c <- true 169 | } 170 | } 171 | return 172 | } 173 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golang structs 2 | Package structs implements simple functions to manipulate structs in Golang. 3 | 4 | [![Documentation](https://godoc.org/github.com/PumpkinSeed/structs?status.svg)](https://godoc.org/github.com/PumpkinSeed/structs) 5 | [![Go Report Card](https://goreportcard.com/badge/github.com/PumpkinSeed/structs)](https://goreportcard.com/report/github.com/PumpkinSeed/structs) 6 | [![license](https://img.shields.io/github/license/yangwenmai/how-to-add-badge-in-github-readme.svg?maxAge=2592000)](github.com/PumpkinSeed/structs/LICENSE.md) 7 | [![Build Status](https://travis-ci.org/PumpkinSeed/structs.svg?branch=master)](https://travis-ci.org/PumpkinSeed/structs) 8 | [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/avelino/awesome-go#utilities) 9 | 10 | 11 | ## Get it 12 | 13 | ``` 14 | go get github.com/PumpkinSeed/structs 15 | ``` 16 | 17 | ## Contains 18 | Contains reports whether value is within struct 19 | 20 | ``` 21 | package main 22 | 23 | import "github.com/PumpkinSeed/structs" 24 | 25 | type Tst struct { 26 | TestString string 27 | TestFloat32 float32 28 | TestFloat64 float64 29 | } 30 | 31 | func main() { 32 | tst := Tst{ 33 | TestString: "test", 34 | TestFloat32: 13.444, 35 | TestFloat64: 16.444, 36 | } 37 | 38 | result := structs.Contains(tst, float64(16.444)) // true 39 | result = structs.Contains(tst, float32(13.444)) // true 40 | } 41 | ``` 42 | 43 | #### Benchmark 44 | 45 | ``` 46 | BenchmarkContains-4 3000000 492 ns/op 47 | ``` 48 | 49 | ## Compare 50 | Compare returns a boolean comparing two struct 51 | 52 | ``` 53 | package main 54 | 55 | import "github.com/PumpkinSeed/structs" 56 | 57 | type TstA struct { 58 | TestInt int 59 | TestInt8 int8 60 | TestInt16 int16 61 | } 62 | 63 | type TstB struct { 64 | TestInt int 65 | TestInt8 int8 66 | TestInt16 int16 67 | } 68 | 69 | func main() { 70 | tstA := TstA{ 71 | TestInt: 12, 72 | TestInt8: 42, 73 | TestInt16: 55, 74 | } 75 | 76 | tstB := TstB{ 77 | TestInt: 12, 78 | TestInt8: 42, 79 | TestInt16: 55, 80 | } 81 | 82 | result := structs.Compare(testStructA, testStructB) // true 83 | } 84 | 85 | 86 | ``` 87 | 88 | #### Benchmark 89 | 90 | ``` 91 | BenchmarkCompareEqual-4 5000000 379 ns/op 92 | BenchmarkCompareNotEqual-4 5000000 372 ns/op 93 | ``` 94 | 95 | ## Index 96 | Index returns the index of the first instance of the value in struct 97 | 98 | ``` 99 | package main 100 | 101 | import "github.com/PumpkinSeed/structs" 102 | 103 | type Tst struct { 104 | TestInt int 105 | TestInt8 int8 106 | TestInt16 int16 107 | TestInt32 int32 108 | TestInt64 int64 109 | TestString string 110 | TestBool bool 111 | TestFloat32 float32 112 | TestFloat64 float64 113 | } 114 | 115 | func main() { 116 | tst := Tst{ 117 | TestInt: 12, 118 | TestInt8: 42, 119 | TestInt16: 55, 120 | TestInt32: 33, 121 | TestInt64: 78, 122 | TestString: "test", 123 | TestBool: false, 124 | TestFloat32: 13.444, 125 | TestFloat64: 16.444, 126 | } 127 | 128 | result := structs.Index(testStruct, "test") // 5 129 | } 130 | ``` 131 | 132 | #### Benchmark 133 | 134 | ``` 135 | BenchmarkIndex-4 5000000 242 ns/op 136 | ``` 137 | 138 | ## FieldNameByValue 139 | FieldNameByValue returns the field's name of the first instance of the value in struct 140 | 141 | ``` 142 | package main 143 | 144 | import "github.com/PumpkinSeed/structs" 145 | 146 | type Tst struct { 147 | TestInt int 148 | TestInt8 int8 149 | TestInt16 int16 150 | TestInt32 int32 151 | TestInt64 int64 152 | TestString string 153 | TestBool bool 154 | TestFloat32 float32 155 | TestFloat64 float64 156 | } 157 | 158 | func main() { 159 | tst := Tst{ 160 | TestInt: 12, 161 | TestInt8: 42, 162 | TestInt16: 55, 163 | TestInt32: 33, 164 | TestInt64: 78, 165 | TestString: "test", 166 | TestBool: false, 167 | TestFloat32: 13.444, 168 | TestFloat64: 16.444, 169 | } 170 | 171 | result := structs.FieldNameByValue(testStruct, "test") // TestString 172 | } 173 | ``` 174 | 175 | #### Benchmark 176 | 177 | ``` 178 | BenchmarkFieldNameByValue-4 5000000 293 ns/op 179 | ``` 180 | 181 | ## Map 182 | The second parameter is a function, apply the function on each field on the struct, or on the condition determined in the third argument 183 | 184 | ``` 185 | package main 186 | 187 | import "github.com/PumpkinSeed/structs" 188 | 189 | type Tst struct { 190 | Username string 191 | Title string 192 | Content string 193 | } 194 | 195 | func main() { 196 | tst := Tst{ 197 | Username: "PumpkinSeed", 198 | Title: "Test title", 199 | Content: "Test content", 200 | } 201 | 202 | result, err := structs.Map(&ts, func(v reflect.Value) error { 203 | if v.Type() == stringType { 204 | v.SetString(strings.ToLower(v.String())) 205 | } 206 | return nil 207 | }) 208 | } 209 | ``` 210 | 211 | #### Benchmark 212 | 213 | ``` 214 | BenchmarkMap-4 5000000 268 ns/op 215 | ``` 216 | 217 | ## Replace 218 | Replace returns a copy of the struct with the first non-overlapping instance of old replaced by new, the last param (n) is the limit, if n < 0, there is no limit on the number of replacements 219 | 220 | ``` 221 | package main 222 | 223 | import "github.com/PumpkinSeed/structs" 224 | 225 | type Tst struct { 226 | TestInt int 227 | TestInt8 int8 228 | TestInt16 int16 229 | TestInt32 int32 230 | TestInt64 int64 231 | TestString1 string 232 | TestString2 string 233 | TestString3 string 234 | TestString4 string 235 | TestBool bool 236 | TestFloat32 float32 237 | TestFloat64 float64 238 | } 239 | 240 | func main() { 241 | tst := Tst{ 242 | TestInt: 12, 243 | TestInt8: 42, 244 | TestInt16: 55, 245 | TestInt32: 33, 246 | TestInt64: 78, 247 | TestString1: "test", 248 | TestString2: "test", 249 | TestString3: "test", 250 | TestString4: "test", 251 | TestBool: false, 252 | TestFloat32: 13.444, 253 | TestFloat64: 16.444, 254 | } 255 | 256 | result, err := structs.Replace(&ts, "test", "new", 2) 257 | } 258 | ``` 259 | 260 | #### Benchmark 261 | 262 | ``` 263 | BenchmarkReplace-4 2000000 655 ns/op 264 | ``` 265 | 266 | ### ToDo 267 | - Upgrade GoDoc 268 | - Implement Map 269 | -------------------------------------------------------------------------------- /structs.go: -------------------------------------------------------------------------------- 1 | // Package structs implements simple functions to manipulate structs in Golang. 2 | package structs 3 | 4 | import ( 5 | "errors" 6 | "reflect" 7 | ) 8 | 9 | var ( 10 | float32Type = reflect.TypeOf(float32(0)) 11 | float64Type = reflect.TypeOf(float64(0)) 12 | 13 | stringType = reflect.TypeOf("") 14 | 15 | intType = reflect.TypeOf(int(0)) 16 | int8Type = reflect.TypeOf(int8(0)) 17 | int16Type = reflect.TypeOf(int16(0)) 18 | int32Type = reflect.TypeOf(int32(0)) 19 | int64Type = reflect.TypeOf(int64(0)) 20 | 21 | uintType = reflect.TypeOf(uint(0)) 22 | uint8Type = reflect.TypeOf(uint8(0)) 23 | uint16Type = reflect.TypeOf(uint16(0)) 24 | uint32Type = reflect.TypeOf(uint32(0)) 25 | uint64Type = reflect.TypeOf(uint64(0)) 26 | // add uintptr 27 | 28 | // add complex types 29 | complex64Type = reflect.TypeOf(complex64(0)) 30 | complex128Type = reflect.TypeOf(complex128(0)) 31 | 32 | // add byte array type 33 | 34 | // add rune type 35 | 36 | boolType = reflect.TypeOf(true) 37 | 38 | errorType = reflect.TypeOf(errors.New("")) 39 | ) 40 | 41 | // Contains reports whether value is within s 42 | func Contains(s, value interface{}) bool { 43 | v := reflect.ValueOf(s) 44 | for i := 0; i < v.NumField(); i++ { 45 | field := v.Field(i) 46 | typeOfField := field.Type() 47 | switch typeOfField { 48 | case stringType: 49 | if containsString(field.String(), value) { 50 | return true 51 | } 52 | case intType, int8Type, int16Type, int32Type, int64Type: 53 | if containsInt(field.Int(), value) { 54 | return true 55 | } 56 | case boolType: 57 | if containsBool(field.Bool(), value) { 58 | return true 59 | } 60 | case float32Type: 61 | if containsFloat32(field.Interface().(float32), value) { 62 | return true 63 | } 64 | case float64Type: 65 | if containsFloat64(field.Interface().(float64), value) { 66 | return true 67 | } 68 | case complex64Type: 69 | if containsComplex64(field.Interface().(complex64), value) { 70 | return true 71 | } 72 | case complex128Type: 73 | if containsComplex128(field.Interface().(complex128), value) { 74 | return true 75 | } 76 | } 77 | } 78 | return false 79 | } 80 | 81 | // Compare returns a boolean comparing two struct 82 | func Compare(a, b interface{}) bool { 83 | return reflect.DeepEqual(a, b) 84 | } 85 | 86 | // Index returns the index of the first instance of the value in s 87 | func Index(s, value interface{}) int { 88 | v1 := reflect.ValueOf(s) 89 | v2 := reflect.ValueOf(value) 90 | 91 | for i := 0; i < v1.NumField(); i++ { 92 | f := v1.Field(i) 93 | if f.Type() == v2.Type() { 94 | switch f.Type() { 95 | case stringType: 96 | if f.String() == v2.String() { 97 | return i 98 | } 99 | case intType, int8Type, int16Type, int32Type, int64Type: 100 | if f.Int() == v2.Int() { 101 | return i 102 | } 103 | case boolType: 104 | if f.Bool() == v2.Bool() { 105 | return i 106 | } 107 | case float32Type: 108 | if f.Interface().(float32) == v2.Interface().(float32) { 109 | return i 110 | } 111 | case float64Type: 112 | if f.Float() == v2.Float() { 113 | return i 114 | } 115 | case complex64Type: 116 | if f.Interface().(complex64) == v2.Interface().(complex64) { 117 | return i 118 | } 119 | case complex128Type: 120 | if f.Complex() == v2.Complex() { 121 | return i 122 | } 123 | } 124 | } 125 | } 126 | return -1 127 | } 128 | 129 | // FieldNameByValue returns the field's name of the first instance of the value in s 130 | func FieldNameByValue(s, value interface{}) string { 131 | v1 := reflect.ValueOf(s) 132 | t1 := reflect.TypeOf(s) 133 | v2 := reflect.ValueOf(value) 134 | 135 | for i := 0; i < v1.NumField(); i++ { 136 | f := v1.Field(i) 137 | if f.Type() == v2.Type() { 138 | switch f.Type() { 139 | case stringType: 140 | if f.String() == v2.String() { 141 | return t1.Field(i).Name 142 | } 143 | case intType, int8Type, int16Type, int32Type, int64Type: 144 | if f.Int() == v2.Int() { 145 | return t1.Field(i).Name 146 | } 147 | case boolType: 148 | if f.Bool() == v2.Bool() { 149 | return t1.Field(i).Name 150 | } 151 | case float32Type: 152 | if f.Interface().(float32) == v2.Interface().(float32) { 153 | return t1.Field(i).Name 154 | } 155 | case float64Type: 156 | if f.Float() == v2.Float() { 157 | return t1.Field(i).Name 158 | } 159 | case complex64Type: 160 | if f.Interface().(complex64) == v2.Interface().(complex64) { 161 | return t1.Field(i).Name 162 | } 163 | case complex128Type: 164 | if f.Complex() == v2.Complex() { 165 | return t1.Field(i).Name 166 | } 167 | } 168 | } 169 | } 170 | return "" 171 | } 172 | 173 | func Map(s interface{}, handler func(reflect.Value) error) (interface{}, error) { 174 | v := reflect.Indirect(reflect.ValueOf(s)) 175 | 176 | for i := 0; i < v.NumField(); i++ { 177 | f := v.Field(i) 178 | if f.CanSet() { 179 | err := handler(f) 180 | if err != nil { 181 | return nil, err 182 | } 183 | } 184 | 185 | } 186 | return s, nil 187 | } 188 | 189 | // Replace returns a copy of the struct s with the first n non-overlapping instance of old replaced by new 190 | func Replace(s, old, new interface{}, n int) (interface{}, error) { 191 | v1 := reflect.Indirect(reflect.ValueOf(s)) 192 | //t1 := reflect.TypeOf(s) 193 | oldV := reflect.ValueOf(old) 194 | newV := reflect.ValueOf(new) 195 | 196 | if oldV.Type() != newV.Type() { 197 | return nil, errReplaceTypesNotMatching 198 | } 199 | 200 | var c = 0 201 | 202 | for i := 0; i < v1.NumField(); i++ { 203 | if n != -1 && c >= n { 204 | return s, nil 205 | } 206 | f := v1.Field(i) 207 | switch f.Type() { 208 | case stringType: 209 | if oldV.Type() == stringType && 210 | f.String() == oldV.String() { 211 | f.SetString(newV.String()) 212 | c++ 213 | } 214 | case intType, int8Type, int16Type, int32Type, int64Type: 215 | if oldV.Type() == intType && 216 | f.Int() == oldV.Int() { 217 | f.SetInt(newV.Int()) 218 | c++ 219 | } 220 | case boolType: 221 | if oldV.Type() == boolType && 222 | f.Bool() == oldV.Bool() { 223 | f.SetBool(newV.Bool()) 224 | c++ 225 | } 226 | case float32Type, float64Type: 227 | if oldV.Type() == float32Type && 228 | f.Float() == oldV.Float() { 229 | f.SetFloat(newV.Float()) 230 | c++ 231 | } 232 | // @todo add float64 233 | case complex64Type, complex128Type: 234 | if oldV.Type() == complex64Type && 235 | f.Complex() == oldV.Complex() { 236 | f.SetComplex(newV.Complex()) 237 | c++ 238 | } 239 | // @todo add complex128 240 | } 241 | } 242 | return s, nil 243 | } 244 | 245 | /* 246 | Contains helper functions 247 | */ 248 | func containsString(s string, v interface{}) bool { 249 | switch v.(type) { 250 | case string: 251 | if v.(string) == s { 252 | return true 253 | } 254 | } 255 | return false 256 | } 257 | 258 | func containsInt(s int64, v interface{}) bool { 259 | switch v.(type) { 260 | case int64: 261 | if v.(int64) == s { 262 | return true 263 | } 264 | case int32: 265 | if int64(v.(int32)) == s { 266 | return true 267 | } 268 | case int16: 269 | if int64(v.(int16)) == s { 270 | return true 271 | } 272 | case int8: 273 | if int64(v.(int8)) == s { 274 | return true 275 | } 276 | case int: 277 | if int64(v.(int)) == s { 278 | return true 279 | } 280 | } 281 | return false 282 | } 283 | 284 | func containsBool(s bool, v interface{}) bool { 285 | switch v.(type) { 286 | case bool: 287 | if v.(bool) == s { 288 | return true 289 | } 290 | } 291 | return false 292 | } 293 | 294 | func containsFloat32(s float32, v interface{}) bool { 295 | switch v.(type) { 296 | case float32: 297 | if v.(float32) == s { 298 | return true 299 | } 300 | } 301 | return false 302 | } 303 | 304 | func containsFloat64(s float64, v interface{}) bool { 305 | switch v.(type) { 306 | case float64: 307 | if v.(float64) == s { 308 | return true 309 | } 310 | } 311 | return false 312 | } 313 | 314 | func containsComplex64(s complex64, v interface{}) bool { 315 | switch v.(type) { 316 | case complex64: 317 | if v.(complex64) == s { 318 | return true 319 | } 320 | } 321 | return false 322 | } 323 | 324 | func containsComplex128(s complex128, v interface{}) bool { 325 | switch v.(type) { 326 | case complex128: 327 | if v.(complex128) == s { 328 | return true 329 | } 330 | } 331 | return false 332 | } 333 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /structs_test.go: -------------------------------------------------------------------------------- 1 | package structs 2 | 3 | import ( 4 | "errors" 5 | "reflect" 6 | "strings" 7 | "testing" 8 | ) 9 | 10 | func TestContains(t *testing.T) { 11 | testStruct := struct { 12 | TestInt int 13 | TestInt8 int8 14 | TestInt16 int16 15 | TestInt32 int32 16 | TestInt64 int64 17 | TestString string 18 | TestBool bool 19 | TestFloat32 float32 20 | TestFloat64 float64 21 | TestComplex64 complex64 22 | TestComplex128 complex128 23 | }{ 24 | TestInt: 12, 25 | TestInt8: 42, 26 | TestInt16: 55, 27 | TestInt32: 33, 28 | TestInt64: 78, 29 | TestString: "test", 30 | TestBool: false, 31 | TestFloat32: 13.444, 32 | TestFloat64: 16.444, 33 | TestComplex64: 12333, 34 | TestComplex128: 123444455, 35 | } 36 | 37 | result := Contains(testStruct, complex64(12333)) 38 | if !result { 39 | t.Error("Should be true instead of false") 40 | } 41 | 42 | result = Contains(testStruct, complex128(123444455)) 43 | if !result { 44 | t.Error("Should be true instead of false") 45 | } 46 | 47 | result = Contains(testStruct, float64(16.444)) 48 | if !result { 49 | t.Error("Should be true instead of false") 50 | } 51 | 52 | result = Contains(testStruct, float32(13.444)) 53 | if !result { 54 | t.Error("Should be true instead of false") 55 | } 56 | 57 | result = Contains(testStruct, 12) 58 | if !result { 59 | t.Error("Should be true instead of false") 60 | } 61 | 62 | result = Contains(testStruct, int8(42)) 63 | if !result { 64 | t.Error("Should be true instead of false") 65 | } 66 | 67 | result = Contains(testStruct, int16(55)) 68 | if !result { 69 | t.Error("Should be true instead of false") 70 | } 71 | 72 | result = Contains(testStruct, int32(33)) 73 | if !result { 74 | t.Error("Should be true instead of false") 75 | } 76 | 77 | result = Contains(testStruct, int64(78)) 78 | if !result { 79 | t.Error("Should be true instead of false") 80 | } 81 | 82 | result = Contains(testStruct, "test") 83 | if !result { 84 | t.Error("Should be true instead of false") 85 | } 86 | 87 | result = Contains(testStruct, false) 88 | if !result { 89 | t.Error("Should be true instead of false") 90 | } 91 | 92 | result = Contains(testStruct, float64(16.445)) 93 | if result { 94 | t.Error("Should be true instead of false") 95 | } 96 | 97 | result = Contains(testStruct, float32(14.444)) 98 | if result { 99 | t.Error("Should be true instead of false") 100 | } 101 | 102 | result = Contains(testStruct, 14) 103 | if result { 104 | t.Error("Should be true instead of false") 105 | } 106 | 107 | result = Contains(testStruct, 44) 108 | if result { 109 | t.Error("Should be true instead of false") 110 | } 111 | 112 | result = Contains(testStruct, 54) 113 | if result { 114 | t.Error("Should be true instead of false") 115 | } 116 | 117 | result = Contains(testStruct, 34) 118 | if result { 119 | t.Error("Should be true instead of false") 120 | } 121 | 122 | result = Contains(testStruct, 79) 123 | if result { 124 | t.Error("Should be true instead of false") 125 | } 126 | 127 | result = Contains(testStruct, "tests") 128 | if result { 129 | t.Error("Should be true instead of false") 130 | } 131 | } 132 | 133 | func TestCompare(t *testing.T) { 134 | testStructA := struct { 135 | TestInt int 136 | TestInt8 int8 137 | TestInt16 int16 138 | }{ 139 | TestInt: 12, 140 | TestInt8: 42, 141 | TestInt16: 55, 142 | } 143 | 144 | testStructB := struct { 145 | TestInt int 146 | TestInt8 int8 147 | TestInt16 int16 148 | }{ 149 | TestInt: 12, 150 | TestInt8: 42, 151 | TestInt16: 55, 152 | } 153 | 154 | if !Compare(testStructA, testStructB) { 155 | t.Errorf("Should return true") 156 | } 157 | 158 | } 159 | 160 | func TestIndex(t *testing.T) { 161 | testStruct := struct { 162 | TestInt int 163 | TestInt8 int8 164 | TestInt16 int16 165 | TestInt32 int32 166 | TestInt64 int64 167 | TestString string 168 | TestBool bool 169 | TestFloat32 float32 170 | TestFloat64 float64 171 | TestComplex64 complex64 172 | TestComplex128 complex128 173 | }{ 174 | TestInt: 12, 175 | TestInt8: 42, 176 | TestInt16: 55, 177 | TestInt32: 33, 178 | TestInt64: 78, 179 | TestString: "test", 180 | TestBool: false, 181 | TestFloat32: 13.444, 182 | TestFloat64: 16.444, 183 | TestComplex64: 12333, 184 | TestComplex128: 123444455, 185 | } 186 | 187 | value := Index(testStruct, "test") 188 | if value != 5 { 189 | t.Errorf("Position should be 5, instead of %d", value) 190 | } 191 | 192 | value = Index(testStruct, 12) 193 | if value != 0 { 194 | t.Errorf("Position should be 0, instead of %d", value) 195 | } 196 | 197 | value = Index(testStruct, false) 198 | if value != 6 { 199 | t.Errorf("Position should be 6, instead of %d", value) 200 | } 201 | 202 | value = Index(testStruct, float32(13.444)) 203 | if value != 7 { 204 | t.Errorf("Position should be 7, instead of %d", value) 205 | } 206 | 207 | value = Index(testStruct, 16.444) 208 | if value != 8 { 209 | t.Errorf("Position should be 8, instead of %d", value) 210 | } 211 | 212 | value = Index(testStruct, complex64(12333)) 213 | if value != 9 { 214 | t.Errorf("Position should be 9, instead of %d", value) 215 | } 216 | 217 | value = Index(testStruct, complex128(123444455)) 218 | if value != 10 { 219 | t.Errorf("Position should be 10, instead of %d", value) 220 | } 221 | 222 | value = Index(testStruct, 42) 223 | if value != -1 { 224 | t.Errorf("Position should be -1, instead of %d", value) 225 | } 226 | } 227 | 228 | func TestFieldNameByValue(t *testing.T) { 229 | testStruct := struct { 230 | TestInt int 231 | TestInt8 int8 232 | TestInt16 int16 233 | TestInt32 int32 234 | TestInt64 int64 235 | TestString string 236 | TestBool bool 237 | TestFloat32 float32 238 | TestFloat64 float64 239 | TestComplex64 complex64 240 | TestComplex128 complex128 241 | }{ 242 | TestInt: 12, 243 | TestInt8: 42, 244 | TestInt16: 55, 245 | TestInt32: 33, 246 | TestInt64: 78, 247 | TestString: "test", 248 | TestBool: false, 249 | TestFloat32: 13.444, 250 | TestFloat64: 16.444, 251 | TestComplex64: 12333, 252 | TestComplex128: 123444455, 253 | } 254 | 255 | value := FieldNameByValue(testStruct, "test") 256 | if value != "TestString" { 257 | t.Errorf("Position should be 'TestString', instead of %s", value) 258 | } 259 | 260 | value = FieldNameByValue(testStruct, 12) 261 | if value != "TestInt" { 262 | t.Errorf("Position should be 'TestInt', instead of %s", value) 263 | } 264 | 265 | value = FieldNameByValue(testStruct, false) 266 | if value != "TestBool" { 267 | t.Errorf("Position should be 'TestBool', instead of %s", value) 268 | } 269 | 270 | value = FieldNameByValue(testStruct, float32(13.444)) 271 | if value != "TestFloat32" { 272 | t.Errorf("Position should be 'TestFloat32', instead of %s", value) 273 | } 274 | 275 | value = FieldNameByValue(testStruct, 16.444) 276 | if value != "TestFloat64" { 277 | t.Errorf("Position should be 'TestFloat64', instead of %s", value) 278 | } 279 | 280 | value = FieldNameByValue(testStruct, complex64(12333)) 281 | if value != "TestComplex64" { 282 | t.Errorf("Position should be 'TestComplex64', instead of %s", value) 283 | } 284 | 285 | value = FieldNameByValue(testStruct, complex128(123444455)) 286 | if value != "TestComplex128" { 287 | t.Errorf("Position should be 'TestComplex128', instead of %s", value) 288 | } 289 | 290 | value = FieldNameByValue(testStruct, 42) 291 | if value != "" { 292 | t.Errorf("Position should be '', instead of %s", value) 293 | } 294 | } 295 | 296 | func TestReplace(t *testing.T) { 297 | type testStruct struct { 298 | TestInt int 299 | TestInt8 int8 300 | TestInt16 int16 301 | TestInt32 int32 302 | TestInt64 int64 303 | TestString1 string 304 | TestString2 string 305 | TestString3 string 306 | TestString4 string 307 | TestBool bool 308 | TestFloat32 float32 309 | TestFloat64 float64 310 | TestComplex64 complex64 311 | TestComplex128 complex128 312 | } 313 | ts := testStruct{ 314 | TestInt: 12, 315 | TestInt8: 42, 316 | TestInt16: 55, 317 | TestInt32: 33, 318 | TestInt64: 78, 319 | TestString1: "test", 320 | TestString2: "test", 321 | TestString3: "test", 322 | TestString4: "test", 323 | TestBool: false, 324 | TestFloat32: 13.444, 325 | TestFloat64: 16.444, 326 | TestComplex64: 12333, 327 | TestComplex128: 123444455, 328 | } 329 | 330 | value, err := Replace(&ts, "test", "new", 2) 331 | if err != nil { 332 | t.Errorf("Error should be nil, instead of %s", err.Error()) 333 | } 334 | if value.(*testStruct).TestString1 != "new" { 335 | t.Errorf("TestString1 should be 'new', instead of %s", value.(*testStruct).TestString1) 336 | } 337 | if value.(*testStruct).TestString3 != "test" { 338 | t.Errorf("TestString1 should be 'test', instead of %s", value.(*testStruct).TestString3) 339 | } 340 | 341 | value, err = Replace(&ts, "test", "new", -1) 342 | if err != nil { 343 | t.Errorf("Error should be nil, instead of %s", err.Error()) 344 | } 345 | if value.(*testStruct).TestString1 != "new" { 346 | t.Errorf("TestString1 should be 'new', instead of %s", value.(*testStruct).TestString1) 347 | } 348 | if value.(*testStruct).TestString3 != "new" { 349 | t.Errorf("TestString1 should be 'new', instead of %s", value.(*testStruct).TestString3) 350 | } 351 | 352 | value, err = Replace(&ts, 12, 42, -1) 353 | if err != nil { 354 | t.Errorf("Error should be nil, instead of %s", err.Error()) 355 | } 356 | if value.(*testStruct).TestInt != 42 { 357 | t.Errorf("TestInt should be 42, instead of %d", value.(*testStruct).TestInt) 358 | } 359 | 360 | value, err = Replace(&ts, false, true, -1) 361 | if err != nil { 362 | t.Errorf("Error should be nil, instead of %s", err.Error()) 363 | } 364 | if value.(*testStruct).TestBool != true { 365 | t.Errorf("TestInt should be true, instead of %t", value.(*testStruct).TestBool) 366 | } 367 | 368 | value, err = Replace(&ts, float32(13.444), float32(42.444), -1) 369 | if err != nil { 370 | t.Errorf("Error should be nil, instead of %s", err.Error()) 371 | } 372 | if value.(*testStruct).TestFloat32 != 42.444 { 373 | t.Errorf("TestFloat32 should be 42.444, instead of %f", value.(*testStruct).TestFloat32) 374 | } 375 | 376 | value, err = Replace(&ts, complex64(12333), complex64(12334), -1) 377 | if err != nil { 378 | t.Errorf("Error should be nil, instead of %s", err.Error()) 379 | } 380 | if value.(*testStruct).TestComplex64 != 12334 { 381 | t.Errorf("TestFloat32 should be 42.444, instead of %f", value.(*testStruct).TestComplex64) 382 | } 383 | 384 | value, err = Replace(&ts, complex64(12333), float32(12334), -1) 385 | if err != errReplaceTypesNotMatching { 386 | t.Errorf("Error should be %s, instead of nil", errReplaceTypesNotMatching.Error()) 387 | } 388 | } 389 | 390 | func TestMap(t *testing.T) { 391 | type testStruct struct { 392 | Username string 393 | Title string 394 | Content string 395 | } 396 | ts := testStruct{ 397 | Username: "PumpkinSeed", 398 | Title: "Test title", 399 | Content: "Test content", 400 | } 401 | res, err := Map(&ts, func(v reflect.Value) error { 402 | if v.Type() == stringType { 403 | v.SetString(strings.ToLower(v.String())) 404 | } 405 | return nil 406 | }) 407 | if err != nil { 408 | t.Errorf("Error should be nil, instead of %s", err.Error()) 409 | } 410 | if res.(*testStruct).Username != "pumpkinseed" { 411 | t.Errorf("Username should be 'pumpkinseed', instead of %s", res.(*testStruct).Username) 412 | } 413 | 414 | var testErr = errors.New("Test") 415 | res, err = Map(&ts, func(v reflect.Value) error { 416 | return testErr 417 | }) 418 | if err != testErr { 419 | t.Errorf("Error should be %s, instead of nil", testErr.Error()) 420 | } 421 | } 422 | 423 | /* 424 | Benchmarks 425 | */ 426 | 427 | func BenchmarkContains(b *testing.B) { 428 | testStruct := struct { 429 | TestInt int 430 | TestInt8 int8 431 | TestInt16 int16 432 | TestInt32 int32 433 | TestInt64 int64 434 | TestString string 435 | TestBool bool 436 | TestFloat32 float32 437 | TestFloat64 float64 438 | }{ 439 | TestInt: 12, 440 | TestInt8: 42, 441 | TestInt16: 55, 442 | TestInt32: 33, 443 | TestInt64: 78, 444 | TestString: "test", 445 | TestBool: false, 446 | TestFloat32: 13.444, 447 | TestFloat64: 16.444, 448 | } 449 | 450 | for n := 0; n < b.N; n++ { 451 | Contains(testStruct, float64(16.444)) 452 | } 453 | } 454 | 455 | func BenchmarkCompareEqual(b *testing.B) { 456 | testStructA := struct { 457 | TestInt int 458 | TestInt8 int8 459 | TestInt16 int16 460 | }{ 461 | TestInt: 12, 462 | TestInt8: 42, 463 | TestInt16: 55, 464 | } 465 | 466 | testStructB := struct { 467 | TestInt int 468 | TestInt8 int8 469 | TestInt16 int16 470 | }{ 471 | TestInt: 12, 472 | TestInt8: 42, 473 | TestInt16: 55, 474 | } 475 | 476 | for n := 0; n < b.N; n++ { 477 | Compare(testStructA, testStructB) 478 | } 479 | } 480 | 481 | func BenchmarkCompareNotEqual(b *testing.B) { 482 | testStructA := struct { 483 | TestInt int 484 | TestInt8 int8 485 | TestInt16 int16 486 | }{ 487 | TestInt: 12, 488 | TestInt8: 42, 489 | TestInt16: 56, 490 | } 491 | 492 | testStructB := struct { 493 | TestInt int 494 | TestInt8 int8 495 | TestInt16 int16 496 | }{ 497 | TestInt: 12, 498 | TestInt8: 42, 499 | TestInt16: 55, 500 | } 501 | 502 | for n := 0; n < b.N; n++ { 503 | Compare(testStructA, testStructB) 504 | } 505 | } 506 | 507 | func BenchmarkIndex(b *testing.B) { 508 | testStruct := struct { 509 | TestInt int 510 | TestInt8 int8 511 | TestInt16 int16 512 | TestInt32 int32 513 | TestInt64 int64 514 | TestString string 515 | TestBool bool 516 | TestFloat32 float32 517 | TestFloat64 float64 518 | }{ 519 | TestInt: 12, 520 | TestInt8: 42, 521 | TestInt16: 55, 522 | TestInt32: 33, 523 | TestInt64: 78, 524 | TestString: "test", 525 | TestBool: false, 526 | TestFloat32: 13.444, 527 | TestFloat64: 16.444, 528 | } 529 | 530 | for n := 0; n < b.N; n++ { 531 | Index(testStruct, "test") 532 | } 533 | } 534 | 535 | func BenchmarkFieldNameByValue(b *testing.B) { 536 | testStruct := struct { 537 | TestInt int 538 | TestInt8 int8 539 | TestInt16 int16 540 | TestInt32 int32 541 | TestInt64 int64 542 | TestString string 543 | TestBool bool 544 | TestFloat32 float32 545 | TestFloat64 float64 546 | }{ 547 | TestInt: 12, 548 | TestInt8: 42, 549 | TestInt16: 55, 550 | TestInt32: 33, 551 | TestInt64: 78, 552 | TestString: "test", 553 | TestBool: false, 554 | TestFloat32: 13.444, 555 | TestFloat64: 16.444, 556 | } 557 | 558 | for n := 0; n < b.N; n++ { 559 | FieldNameByValue(testStruct, "test") 560 | } 561 | } 562 | 563 | func BenchmarkReplace(b *testing.B) { 564 | type testStruct struct { 565 | TestInt64 int64 566 | TestString1 string 567 | TestString2 string 568 | TestString3 string 569 | TestString4 string 570 | TestBool bool 571 | TestFloat32 float32 572 | TestFloat64 float64 573 | TestComplex64 complex64 574 | TestComplex128 complex128 575 | } 576 | ts := testStruct{ 577 | TestInt64: 78, 578 | TestString1: "test", 579 | TestString2: "test", 580 | TestString3: "test", 581 | TestString4: "test", 582 | TestBool: false, 583 | TestFloat32: 13.444, 584 | TestFloat64: 16.444, 585 | TestComplex64: 12333, 586 | TestComplex128: 123444455, 587 | } 588 | for n := 0; n < b.N; n++ { 589 | Replace(&ts, "test", "new", 2) 590 | } 591 | } 592 | 593 | func BenchmarkMap(b *testing.B) { 594 | type testStruct struct { 595 | Username string 596 | Title string 597 | Content string 598 | } 599 | ts := testStruct{ 600 | Username: "PumpkinSeed", 601 | Title: "Test title", 602 | Content: "Test content", 603 | } 604 | for n := 0; n < b.N; n++ { 605 | Map(&ts, func(v reflect.Value) error { 606 | if v.Type() == stringType { 607 | v.SetString(strings.ToLower(v.String())) 608 | } 609 | return nil 610 | }) 611 | } 612 | } 613 | --------------------------------------------------------------------------------