Created
June 30, 2018 09:44
-
-
Save mashingan/c15d8d0e0bec792ecac7b8fd88cb1074 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// from:: | |
// http://stackoverflow.com/questions/11527935/ddg#11544854 | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"reflect" | |
) | |
type M map[string]interface{} // just an alias | |
var Record = []byte(`{ "bit_size": 8, "secret_key": false }`) | |
type DB struct { | |
NumBits int `json:"bit_size"` | |
Secret bool `json:"secret_key"` | |
} | |
type User struct { | |
NumBits int `json:"num_bits"` | |
} | |
func main() { | |
d := new(DB) | |
e := json.Unmarshal(Record, d) | |
if e != nil { | |
panic(e) | |
} | |
m := mapFields(d) | |
fmt.Println("Mapped fields: ", m) | |
u := new(User) | |
o := applyMap(u, m) | |
fmt.Println("Applied map: ", o) | |
j, e := json.Marshal(o) | |
if e != nil { | |
panic(e) | |
} | |
fmt.Println("Output JSON: ", string(j)) | |
} | |
func applyMap(u *User, m M) M { | |
t := reflect.TypeOf(u).Elem() | |
o := make(M) | |
for i := 0; i < t.NumField(); i++ { | |
f := t.FieldByIndex([]int{i}) | |
// skip unexported fields | |
if f.PkgPath != "" { | |
continue | |
} | |
if x, ok := m[f.Name]; ok { | |
k := f.Tag.Get("json") | |
o[k] = x | |
} | |
} | |
return o | |
} | |
func mapFields(x *DB) M { | |
o := make(M) | |
v := reflect.ValueOf(x).Elem() | |
t := v.Type() | |
for i := 0; i < v.NumField(); i++ { | |
f := t.FieldByIndex([]int{i}) | |
// skip unexported fields | |
if f.PkgPath != "" { | |
continue | |
} | |
o[f.Name] = v.FieldByIndex([]int{i}).Interface() | |
} | |
return o | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment