Skip to content

Commit

Permalink
Merge pull request #1 from NICEXAI/hotfix-name-error
Browse files Browse the repository at this point in the history
fix: fix struct name error
  • Loading branch information
NICEXAI authored Sep 22, 2021
2 parents 1f19800 + bae6a15 commit d2f80b9
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 19 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
_ = json.Unmarshal(bData, &options)

//map to struct
structTxt := go2struct.Map2Struct(options)
structTxt := go2struct.Map2Struct("message", options)

fmt.Printf("map to struct success, result: \n%s", structTxt)

Expand All @@ -63,7 +63,7 @@ func main() {
}
`

res, err := go2struct.JSON2Struct([]byte(jsonTemp))
res, err := go2struct.JSON2Struct("message", []byte(jsonTemp))
if err != nil {
fmt.Printf("json to struct failed, error: \n%v", err)
return
Expand All @@ -78,12 +78,11 @@ msg:
content: Hello
`

res, err = go2struct.YAML2Struct([]byte(temp))
res, err = go2struct.YAML2Struct("message", []byte(temp))
if err != nil {
fmt.Printf("yaml to struct failed, error: \n%v", err)
return
}
fmt.Printf("yaml to struct success, result: \n%s", res)
}

```
6 changes: 3 additions & 3 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
_ = json.Unmarshal(bData, &options)

//map to struct
structTxt := go2struct.Map2Struct(options)
structTxt := go2struct.Map2Struct("message", options)

fmt.Printf("map to struct success, result: \n%s", structTxt)

Expand All @@ -45,7 +45,7 @@ func main() {
}
`

res, err := go2struct.JSON2Struct([]byte(jsonTemp))
res, err := go2struct.JSON2Struct("message", []byte(jsonTemp))
if err != nil {
fmt.Printf("json to struct failed, error: \n%v", err)
return
Expand All @@ -60,7 +60,7 @@ msg:
content: Hello
`

res, err = go2struct.YAML2Struct([]byte(temp))
res, err = go2struct.YAML2Struct("message", []byte(temp))
if err != nil {
fmt.Printf("yaml to struct failed, error: \n%v", err)
return
Expand Down
6 changes: 3 additions & 3 deletions json2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package go2struct
import "encoding/json"

// JSON2Struct convert json to struct
func JSON2Struct(data []byte) (string, error) {
func JSON2Struct(name string, data []byte) ([]byte, error) {
var m map[string]interface{}

if err := json.Unmarshal(data, &m); err != nil {
return "", err
return nil, err
}

return Map2Struct(m), nil
return Map2Struct(name, m), nil
}
2 changes: 1 addition & 1 deletion json2struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestJSON2Struct(t *testing.T) {
}
`

res, err := JSON2Struct([]byte(temp))
res, err := JSON2Struct("message", []byte(temp))
if err != nil {
t.Fatalf("json to struct failed, error: \n%v", err)
return
Expand Down
6 changes: 3 additions & 3 deletions map2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const (
)

// Map2Struct convert map to struct
func Map2Struct(m map[string]interface{}) string {
cellNodes := convertMapToCellNode("message", m, 0)
return strings.Join(cellNodes, "")
func Map2Struct(name string, m map[string]interface{}) []byte {
cellNodes := convertMapToCellNode(name, m, 0)
return []byte(strings.Join(cellNodes, ""))
}

func convertMapToCellNode(name string, m map[string]interface{}, tier int) (cn []string) {
Expand Down
2 changes: 1 addition & 1 deletion map2struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ func TestMap2Struct(t *testing.T) {
return
}
_ = json.Unmarshal(bData, &options)
t.Logf("map to struct success, result: \n%s", Map2Struct(options))
t.Logf("map to struct success, result: \n%s", Map2Struct("message", options))
}
6 changes: 3 additions & 3 deletions yaml2struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package go2struct
import "gopkg.in/yaml.v2"

// YAML2Struct convert yaml to struct
func YAML2Struct(data []byte) (string, error) {
func YAML2Struct(name string, data []byte) ([]byte, error) {
var m map[string]interface{}

if err := yaml.Unmarshal(data, &m); err != nil {
return "", err
return nil, err
}

return Map2Struct(m), nil
return Map2Struct(name, m), nil
}
2 changes: 1 addition & 1 deletion yaml2struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ msg:
content: Hello
`

res, err := YAML2Struct([]byte(temp))
res, err := YAML2Struct("message", []byte(temp))
if err != nil {
t.Fatalf("yaml to struct failed, error: \n%v", err)
return
Expand Down

0 comments on commit d2f80b9

Please sign in to comment.