Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
715 additions
and
0 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
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,46 @@ | ||
## jy2struct | ||
|
||
json和yaml转go struct代码。 | ||
|
||
<br> | ||
|
||
### 安装 | ||
|
||
> go get -u github.com/zhufuyi/pkg/jy2struct | ||
<br> | ||
|
||
### 使用示例 | ||
|
||
主要设置参数: | ||
|
||
```go | ||
type Args struct { | ||
Format string // 文档格式,json或yaml | ||
Data string // json或yaml内容 | ||
InputFile string // 文件 | ||
Name string // 结构体名称 | ||
SubStruct bool // 子结构体是否分开 | ||
Tags string // 添加额外tag,多个tag用逗号分隔 | ||
} | ||
``` | ||
|
||
<br> | ||
|
||
转换示例: | ||
|
||
```go | ||
// json转struct | ||
code, err := jy2struct.Covert(&jy2struct.Args{ | ||
Format: "json", | ||
// InputFile: "user.json", // 来源于json文件 | ||
SubStruct: true, | ||
}) | ||
|
||
// json转struct | ||
code, err := jy2struct.Covert(&jy2struct.Args{ | ||
Format: "yaml", | ||
// InputFile: "user.yaml", // 来源于yaml文件 | ||
SubStruct: true, | ||
}) | ||
``` |
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,77 @@ | ||
package jy2struct | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io/ioutil" | ||
"strings" | ||
) | ||
|
||
// Args 参数 | ||
type Args struct { | ||
Format string // 文档格式,json或yaml | ||
Data string // json或yaml内容 | ||
InputFile string // 文件 | ||
Name string // 结构体名称 | ||
SubStruct bool // 子结构体是否分开 | ||
Tags string // 字段tag,多个tag用逗号分隔 | ||
|
||
tags []string | ||
convertFloats bool | ||
parser Parser | ||
} | ||
|
||
func (j *Args) checkValid() error { | ||
switch j.Format { | ||
case "json": | ||
j.parser = ParseJson | ||
j.convertFloats = true | ||
case "yaml": | ||
j.parser = ParseYaml | ||
default: | ||
return errors.New("format must be json or yaml") | ||
} | ||
|
||
j.tags = []string{j.Format} | ||
tags := strings.Split(j.Tags, ",") | ||
for _, tag := range tags { | ||
if tag == j.Format || tag == "" { | ||
continue | ||
} | ||
j.tags = append(j.tags, tag) | ||
} | ||
|
||
if j.Name == "" { | ||
j.Name = "GenerateName" | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Covert json或yaml转go struct | ||
func Covert(args *Args) (string, error) { | ||
err := args.checkValid() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var data []byte | ||
if args.Data != "" { | ||
data = []byte(args.Data) | ||
} else { | ||
// 读取文件 | ||
data, err = ioutil.ReadFile(args.InputFile) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
input := bytes.NewReader(data) | ||
|
||
output, err := Generate(input, args.parser, args.Name, "main", args.tags, args.SubStruct, args.convertFloats) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(output), nil | ||
} |
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,63 @@ | ||
package jy2struct | ||
|
||
import "testing" | ||
|
||
func TestCovert(t *testing.T) { | ||
type args struct { | ||
args *Args | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "json to struct from data", | ||
args: args{args: &Args{ | ||
Data: `{"name":"zhangsan","age":22}`, | ||
Format: "json", | ||
}}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "yaml to struct from data", | ||
args: args{args: &Args{ | ||
Data: ` | ||
name": zhangsan | ||
first_name: zhu | ||
age: 10 | ||
`, | ||
Format: "yaml", | ||
}}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "json to struct from file", | ||
args: args{args: &Args{ | ||
InputFile: "test.json", | ||
Format: "json", | ||
SubStruct: true, | ||
}}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "yaml to struct from file", | ||
args: args{args: &Args{ | ||
InputFile: "test.yaml", | ||
Format: "yaml", | ||
SubStruct: true, | ||
}}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := Covert(tt.args.args) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Covert() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
t.Log(got) | ||
}) | ||
} | ||
} |
Oops, something went wrong.