@@ -12,83 +12,77 @@ import (
1212)
1313
1414const (
15- GenInt32 generator. TaskName = "GenInt32"
15+ GenInt32 string = "GenInt32"
1616)
1717
1818type genInt32Params struct {
1919 min int32
2020 max int32
2121}
2222
23- type GenInt32Task struct {}
24-
25- // Ensure GenInt32Task implements Task.
26- var _ generator. Task = & GenInt32Task { }
23+ type GenInt32Task struct {
24+ generator. BaseTask
25+ numberConfig config. NumberRangeConfig
26+ }
2727
28- // Tags implements Task.
29- func ( g * GenInt32Task ) Name () string {
30- return string ( GenInt32 )
28+ type GenInt32TaskInstance struct {
29+ GenInt32Task
30+ genFunc generator. GenerationFunction
3131}
3232
33- // GenerationFunction implements Task.
34- func (g * GenInt32Task ) GenerationFunction (
35- taskProperties generator.TaskProperties ,
36- ) generator.GenerationFunction {
37- generator .ValidateParamCount (g , taskProperties )
38- params := g .getInt32Params (taskProperties .FieldName , taskProperties .Parameters )
39- numberConfig := config .NewNumberRangeConfig ()
40- numberConfig .Int ().SetRange (int (params .min ), int (params .max ))
41- return core.GenerateNumberFunc [int ](numberConfig )
33+ var (
34+ _ generator.TaskInstance = & GenInt32TaskInstance {}
35+ // Ensure GenInt32Task implements Task.
36+ _ generator.Task = & GenInt32Task {}
37+ )
38+
39+ func NewGenInt32Task () * GenInt32Task {
40+ gt := & GenInt32Task {
41+ numberConfig : config .NewNumberRangeConfig (),
42+ BaseTask : * generator .NewBaseTask (string (GenInt32 ), 2 ),
43+ }
44+ return gt
4245}
4346
44- func (g * GenInt32Task ) ExpectedParameterCount () int {
45- return 2
47+ func (g GenInt32Task ) Instance (params ... string ) generator.TaskInstance {
48+ gt := & GenInt32TaskInstance {
49+ GenInt32Task : g ,
50+ }
51+ gt .numberConfig = g .numberConfig .Copy ()
52+ gt .SetParameters (params ... )
53+ gt .genFunc = core.GenerateNumberFunc [int32 ](gt .numberConfig )
54+ return gt
4655}
4756
48- func (g * GenInt32Task ) FunctionHolder ( taskProperties generator.TaskProperties ) core. FunctionHolder {
49- return core . NewFunctionHolderNoArgs ( g . GenerationFunction ( taskProperties ))
57+ func (g * GenInt32TaskInstance ) GenerationFunction () generator.GenerationFunction {
58+ return g . genFunc
5059}
5160
52- func (g * GenInt32Task ) getInt32Params (fieldName string , params []string ) genInt32Params {
53- param_1 , err := strconv .Atoi (params [0 ])
61+ func (g * GenInt32TaskInstance ) SetParameters (params ... string ) {
62+ g .ValidateParamCount (params ... )
63+ min , err := strconv .Atoi (params [0 ])
5464 if err != nil {
55- panic (fmt .Sprintf ("error with field %s: task %s error: %s" , fieldName , GenInt32 , err ))
65+ panic (fmt .Sprintf ("Error parsing min value: %s" , err ))
5666 }
57-
58- param_2 , err := strconv .Atoi (params [1 ])
67+ max , err := strconv .Atoi (params [1 ])
5968 if err != nil {
60- panic (fmt .Sprintf ("error with field %s: task %s error: %s" , fieldName , GenInt32 , err ))
61- }
62-
63- if param_1 > param_2 {
64- err = fmt .Errorf (
65- "min must be less or equal to the max value min = %d max = %d" ,
66- param_1 ,
67- param_2 ,
68- )
69- panic (fmt .Sprintf ("error with field %s: task %s error: %s" , fieldName , GenInt32 , err ))
70-
69+ panic (fmt .Sprintf ("Error parsing max value: %s" , err ))
7170 }
7271
73- return genInt32Params {
74- min : int32 (param_1 ),
75- max : int32 (param_2 ),
72+ if min > max {
73+ panic (fmt .Sprintf ("min value %d is greater than max value %d" , min , max ))
7674 }
75+ g .numberConfig .Int32 ().SetRange (int32 (min ), int32 (max ))
7776}
7877
7978type M struct {
8079 Name string
8180}
8281
8382type Person struct {
84- // Love ABC
8583 Age * int
8684 Time time.Time
8785 Other * M
88-
89- // Parray []Person
90-
91- // Person *Person
9286}
9387
9488type P struct {
@@ -97,72 +91,20 @@ type P struct {
9791}
9892
9993type Test struct {
100- // A *int
101- // S string
102- C int
103- Person P `json:"person"`
104-
94+ B int32 `gen_task:"GenInt32(2)" gen_task_1:"10" gen_task_2:"20"`
95+ C int32
96+ Person P
10597 Cpoint * int
10698 T time.Time
107-
108- // Pa []Pa
109- // Parray []Person
110-
111- // L
11299}
113100
114101func main () {
115- // e := dstruct.ExtendStruct(Test{}).Build().Instance()
116- // createTime := time.Now()
117- // // for i := 0; i < 1; i++ {
118- // // estruct.AddField(fmt.Sprintf("Test_%d", i), Test{}, "")
119- // // }
120- // fmt.Println(time.Since(createTime))
121- // // b := estruct.Build()
122- // //
123- // st := config.GenerationSettings{SetNonRequiredFields: true}
124- // gestruct := dstruct.NewGeneratedStructWithConfig(e,
125- // config.NewDstructConfig(),
126- // st,
127- // )
128- //
129- // c := gestruct.GetGenerationConfig()
130- // c.SetIntRange(20, 50)
131- // st.SetNonRequiredFields = false
132- //
133- // // gestruct.SetFieldGenerationSettings("Person", st)
134- // // gestruct.SetFieldGenerationConfig("Person", c)
135- //
136- // gTime := time.Now()
137- // gestruct.Generate()
138- // // gestruct.Update()
139- // // gestruct.Set("Person.P", Person{Age: new(int), Time: time.Now()})
140- // err := gestruct.Set(
141- // "Person.P",
142- // Person{Age: new(int), Time: time.Now(), Other: &M{Name: "Martin"}},
143- // )
144- // if err != nil {
145- // // panic(err)
146- // }
147- //
148- // fmt.Println("Time to generate: ", time.Since(gTime))
149- // fmt.Printf("%+v\n", gestruct.Get_("Person.P.Other.Name"))
150- // for field := range gestruct.GetFields() {
151- // fmt.Println("Field: ", field)
152- // fmt.Printf(
153- // "'%s': dstruct: %+v goType: %+v\n",
154- // field,
155- // value.GetDstructType(),
156- // value.GetGoType(),
157- // )
158- // }
159-
160102 generatedStuct := dstruct .NewGeneratedStructWithConfig (
161103 Test {Cpoint : new (int )},
162104 config .NewDstructConfig ().SetSliceLength (3 , 3 ),
163105 config .DefaultGenerationSettings (),
164106 )
165- gt := & GenInt32Task {}
107+ gt := NewGenInt32Task ()
166108 generator .AddTask (gt )
167109 if err := generatedStuct .Set ("Person.P" , Person {}); err != nil {
168110 panic (err )
@@ -171,12 +113,14 @@ func main() {
171113 "Person.Value" ,
172114 config .NewDstructConfig ().SetIntRange (800 , 1000 ),
173115 )
174-
175- generatedStuct .SetFieldFromTask ("C" , gt , 300 , 400 )
176- // generatedStuct.Update()
116+ gti := gt .Instance ("20" , "30" ).(* GenInt32TaskInstance )
117+ generatedStuct .SetFieldFromTaskInstance ("C" , gti )
177118 generatedStuct .Generate ()
178119
179120 fmt .Printf ("%+v\n " , generatedStuct )
121+ gti .SetParameters ("100" , "200" )
180122
181- fmt .Println ("Testing task" )
123+ generatedStuct .Generate ()
124+
125+ fmt .Printf ("%+v\n " , generatedStuct )
182126}
0 commit comments