Skip to content

Commit 70ae2a7

Browse files
committed
feat: args could resolve all slice. struct, string or other kind, and will be replaced if value is variable's key(#master)
1 parent 404ca3c commit 70ae2a7

2 files changed

Lines changed: 106 additions & 16 deletions

File tree

gcg.go

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
version = "0.0.6"
18+
version = "0.0.7"
1919
helpText = "Using `gcg <json file>` to generate go file\nSuch as `gcg data.json`"
2020
)
2121

@@ -41,8 +41,8 @@ type goFile struct {
4141
}
4242

4343
type bodyArea struct {
44-
Template interface{} `json:"template"`
45-
Args []interface{} `json:"args"`
44+
Template interface{} `json:"template"`
45+
Args interface{} `json:"args"`
4646
}
4747
type jsonMap = map[string]interface{}
4848

@@ -161,9 +161,41 @@ func readData(filename string) (cfg config) {
161161

162162
filenameSlice := strings.Split(filename, "/")
163163
cfg.Root = strings.Join(filenameSlice[0:len(filenameSlice)-1], "/")
164+
165+
for fileIndex, file := range cfg.Files {
166+
for bodyIndex, body := range file.Body {
167+
cfg.Files[fileIndex].Body[bodyIndex].Args = modifyVariable(body.Args, cfg.Variable)
168+
}
169+
}
164170
return
165171
}
166172

173+
func modifyVariable(args interface{}, variable map[string]interface{}) interface{} {
174+
switch reflect.TypeOf(args).Kind() {
175+
case reflect.String:
176+
s, ok := variable[args.(string)]
177+
if ok {
178+
return s
179+
}
180+
return args
181+
case reflect.Slice:
182+
s := make([]interface{}, 0)
183+
for _, arg := range args.([]interface{}) {
184+
s = append(s, modifyVariable(arg, variable))
185+
}
186+
return s
187+
case reflect.Map:
188+
m := make(map[string]interface{})
189+
iter := reflect.ValueOf(args).MapRange()
190+
for iter.Next() {
191+
m[iter.Key().String()] = modifyVariable(iter.Value().Interface(), variable)
192+
}
193+
return m
194+
default:
195+
return args
196+
}
197+
}
198+
167199
// importPackage from a string or []string to generator import part
168200
func importPackage(x interface{}) (s string) {
169201
switch x.(type) {
@@ -196,25 +228,22 @@ func getFileName(path string) (filename string) {
196228
}
197229

198230
// renderTemplate render the block template
199-
func renderTemplate(buf io.Writer, templates []string, args []interface{}, variable map[string]interface{}) {
231+
func renderTemplate(buf io.Writer, templates []string, args interface{}, variable map[string]interface{}) {
200232
templateName := getFileName(templates[0])
201233
tpl, err := template.New(templateName).Funcs(funcMap).ParseFiles(templates...)
202234
exitWhenError(err)
203-
for _, arg := range args {
204-
// tpl.Execute(buf, arg)
205235

206-
var temp interface{}
207-
s, ok := arg.(string)
208-
if ok {
209-
temp, ok = variable[s]
210-
}
211-
if !ok {
212-
temp = arg
213-
}
236+
if reflect.TypeOf(args).Kind() == reflect.Slice {
237+
argsSlice, _ := args.([]interface{})
238+
for _, arg := range argsSlice {
239+
// tpl.Execute(buf, arg)
214240

215-
err = tpl.ExecuteTemplate(buf, templateName, temp)
241+
err = tpl.ExecuteTemplate(buf, templateName, arg)
242+
exitWhenError(err)
243+
}
244+
} else {
245+
err = tpl.ExecuteTemplate(buf, templateName, args)
216246
exitWhenError(err)
217-
// buf.Write([]byte{'\n'})
218247
}
219248
}
220249

gcg_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"os"
8+
"reflect"
89
"testing"
910
)
1011

@@ -132,3 +133,63 @@ func Test_renderTemplate(t *testing.T) {
132133
}
133134

134135
}
136+
137+
func Test_modifyVariable(t *testing.T) {
138+
type args struct {
139+
args interface{}
140+
variable map[string]interface{}
141+
}
142+
tests := []struct {
143+
name string
144+
args args
145+
want interface{}
146+
}{
147+
{
148+
name: "do nothing",
149+
args: args{
150+
args: []interface{}{"a", "b", "c"},
151+
variable: map[string]interface{}{},
152+
},
153+
want: []interface{}{"a", "b", "c"},
154+
},
155+
{
156+
name: "modify direct string",
157+
args: args{
158+
args: "$variable",
159+
variable: map[string]interface{}{"$variable": []interface{}{"a", "b", "c"}},
160+
},
161+
want: []interface{}{"a", "b", "c"},
162+
},
163+
{
164+
name: "modify in slice",
165+
args: args{
166+
args: []interface{}{"$variable", "variable"},
167+
variable: map[string]interface{}{"$variable": []interface{}{"a", "b", "c"}},
168+
},
169+
want: []interface{}{[]interface{}{"a", "b", "c"}, "variable"},
170+
},
171+
{
172+
name: "modify in map",
173+
args: args{
174+
args: map[string]interface{}{"key": "value", "variable": "$variable"},
175+
variable: map[string]interface{}{"$variable": []interface{}{"a", "b", "c"}},
176+
},
177+
want: map[string]interface{}{"key": "value", "variable": []interface{}{"a", "b", "c"}},
178+
},
179+
{
180+
name: "mix",
181+
args: args{
182+
args: map[string]interface{}{"key": "value", "variable": []interface{}{"$variable", "variable"}},
183+
variable: map[string]interface{}{"$variable": []interface{}{"a", "b", "c"}},
184+
},
185+
want: map[string]interface{}{"key": "value", "variable": []interface{}{[]interface{}{"a", "b", "c"}, "variable"}},
186+
},
187+
}
188+
for _, tt := range tests {
189+
t.Run(tt.name, func(t *testing.T) {
190+
if got := modifyVariable(tt.args.args, tt.args.variable); !reflect.DeepEqual(got, tt.want) {
191+
t.Errorf("modifyVariable() = %v, want %v", got, tt.want)
192+
}
193+
})
194+
}
195+
}

0 commit comments

Comments
 (0)