@@ -25,7 +25,7 @@ type DStructGeneratedStruct[T any] struct {
2525 fieldContexts GeneratedFieldContexts
2626 structConfig core.GeneratedFieldConfig
2727 instance T
28- customTypes map [reflect. Type ]core.FunctionHolder
28+ customTypes map [string ]core.FunctionHolder
2929}
3030
3131var _ GeneratedStruct = & DStructGeneratedStruct [int ]{}
@@ -50,11 +50,11 @@ func NewGeneratedStructWithConfig[T any](val T,
5050 cfg ,
5151 settings ,
5252 ),
53- customTypes : make (map [reflect. Type ]core.FunctionHolder ),
53+ customTypes : make (map [string ]core.FunctionHolder ),
5454 }
5555
5656 for _ , v := range customTypes {
57- generatedStruct .addCustomType (v )
57+ generatedStruct .AddCustomType (v )
5858 }
5959 generatedStruct .addCustomTypes ()
6060 generatedStruct .populateGeneratedFields (generatedStruct .root )
@@ -74,6 +74,11 @@ func (gs *DStructGeneratedStruct[T]) NewT() *T {
7474
7575// Generate implements GeneratedStruct.Generate.
7676func (gs * DStructGeneratedStruct [T ]) Generate () {
77+ // TODO: should only be called if new struct fields are generated
78+ // an idea is to have generatedFields return a bool to indicate if new fields were generated
79+ // also make this method private
80+
81+ // gs.populateGeneratedFields(gs.root) // in case new fields are added /
7782 gs .generateFields ()
7883
7984 switch any (* new (T )).(type ) {
@@ -83,10 +88,14 @@ func (gs *DStructGeneratedStruct[T]) Generate() {
8388 }
8489
8590 gs .instance = toType [T ](gs .DynamicStructModifierImpl )
91+ }
8692
87- // TODO: should only be called if new struct fields are generated
88- // an idea is to have generatedFields return a bool to indicate if new fields were generated
89- gs .Update ()
93+ func (gs * DStructGeneratedStruct [T ]) Set (field string , value any ) error {
94+ if err := gs .DynamicStructModifierImpl .Set (field , value ); err != nil {
95+ return err
96+ }
97+ gs .populateGeneratedFields (gs .fieldNodeMap [field ].parent )
98+ return nil
9099}
91100
92101// SetFieldGenerationSettings implements GeneratedStruct.SetFieldGenerationSettings
@@ -148,7 +157,7 @@ func (gs *DStructGeneratedStruct[T]) SetFieldGenerationConfig(
148157 return err
149158 }
150159
151- if gs .fieldContexts [field ] == nil {
160+ if gs .fieldContexts [field ] == nil { // if field has children
152161 gs .propagateConfig (gs .fieldNodeMap [field ], cfg )
153162 } else {
154163 // this will be fields that are either custom types or fields that have no children
@@ -257,24 +266,18 @@ func (gs *DStructGeneratedStruct[T]) SetGenerationFunctions(
257266 gs .propagateGenerationFunctions (gs .root , functions )
258267}
259268
260- // SetFieldFromTask implements GeneratedStruct.SetFieldFromTask
261- func (gs * DStructGeneratedStruct [T ]) SetFieldFromTask (
269+ // SetFieldFromTaskInstance implements GeneratedStruct.SetFieldFromTaskInstanc
270+ func (gs * DStructGeneratedStruct [T ]) SetFieldFromTaskInstance (
262271 field string ,
263- task generator.Task ,
264- params ... any ,
272+ taskInstance generator.TaskInstance ,
265273) error {
266- taskProperties , err := generator .CreateTaskProperties (
267- field ,
268- generator .GetTagForTask (generator .TaskName (task .Name ()), params ... ),
269- )
270- if err != nil {
271- return err
272- }
273-
274274 gs .SetFieldGenerationFunction (
275275 field ,
276- core .NewFunctionHolderNoArgs (task .GenerationFunction (* taskProperties )),
276+ core .NewFunctionHolderNoArgs (
277+ taskInstance .GenerationFunction (),
278+ ),
277279 )
280+
278281 return nil
279282}
280283
@@ -311,35 +314,41 @@ func toPointerType[T any](gs DynamicStructModifier) *T {
311314}
312315
313316func (gs * DStructGeneratedStruct [T ]) generateFields () {
317+ // wg := sync.WaitGroup{}
314318 for k , genFunc := range gs .fieldContexts {
319+ // wg.Add(1)
320+ // go func(k string, genFunc *core.GeneratedFieldContext) {
315321 if err := gs .Set (k , genFunc .Generate ()); err != nil {
316322 fmt .Println (err )
317323 }
324+ // wg.Done()
325+ // }(k, genFunc)
318326 }
327+ // wg.Wait()
319328}
320329
321330func (gs * DStructGeneratedStruct [T ]) addCustomTypes () {
322- gs .addCustomType (
331+ gs .AddCustomType (
323332 CustomType {
324333 time.Time {},
325334 core .DefaultDateFunctionHolder (gs .structConfig .GenerationConfig .Date ()),
326335 },
327336 )
328337}
329338
330- func (gs * DStructGeneratedStruct [T ]) addCustomType (customType CustomType ) {
339+ func (gs * DStructGeneratedStruct [T ]) AddCustomType (customType CustomType ) {
331340 // TODO: restrict some types from being added such as nil, ints etc
332- gs .customTypes [reflect .TypeOf (customType .Value )] = customType .FunctionHolder
341+ gs .customTypes [reflect .TypeOf (customType .Value ). String () ] = customType .FunctionHolder
333342 // the function holder kind is the find that the function retrusn which could either be an existing kind or a new kind i.ie time.Time would be a new kind
334343 // TODO:idea: GenerationsFunction key should be type of CustomKind and not reflect.Kind
335344 gs .structConfig .GenerationFunctions [customType .FunctionHolder .Kind ()] = customType .FunctionHolder
336345}
337346
338347func (gs * DStructGeneratedStruct [T ]) createGeneratedField (
339- field * Node [structField ],
348+ field * Node [StructField ],
340349 kind reflect.Kind ,
341350) * core.GeneratedField {
342- v := core .NewGeneratedField (field .data .fullyQualifiedName ,
351+ v := core .NewGeneratedField (field .data .qualifiedName ,
343352 field .data .value ,
344353 field .data .tag ,
345354 gs .structConfig .Copy (kind ),
@@ -348,75 +357,77 @@ func (gs *DStructGeneratedStruct[T]) createGeneratedField(
348357 return v
349358}
350359
351- func (gs * DStructGeneratedStruct [T ]) populateGeneratedFields (node * Node [structField ]) {
360+ func (gs * DStructGeneratedStruct [T ]) populateGeneratedFields (node * Node [StructField ]) {
352361 for _ , field := range node .children {
362+ if gs .fieldContexts [field .data .qualifiedName ] != nil {
363+ continue
364+ }
365+
353366 if customType := gs .customTypes [field .data .goType ]; customType != nil {
354- gs .fieldContexts [field .data .fullyQualifiedName ] = core .NewGeneratedFieldContext (
367+ gs .fieldContexts [field .data .qualifiedName ] = core .NewGeneratedFieldContext (
355368 gs .createGeneratedField (field , customType .Kind ()),
356369 )
357370 } else if field .HasChildren () {
358371 gs .populateGeneratedFields (field )
359372 } else {
360- gs .fieldContexts [field .data .fullyQualifiedName ] = core .NewGeneratedFieldContext (
373+ gs .fieldContexts [field .data .qualifiedName ] = core .NewGeneratedFieldContext (
361374 gs .createGeneratedField (field , field .data .value .Kind ()),
362375 )
363376 }
364377 }
365378}
366379
367380func (gs * DStructGeneratedStruct [T ]) propagateConfig (
368- node * Node [structField ],
381+ node * Node [StructField ],
369382 cfg config.Config ,
370383) {
371384 for _ , field := range node .children {
372385 // Don't propagate changes to children nodes if the field is a custom type
373- if field .HasChildren () &&
374- ! gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .IsCustomType () {
386+ if field .HasChildren () && gs .customTypes [field .data .goType ] == nil {
375387 gs .propagateConfig (field , cfg )
376388 } else {
377- gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .SetConfig (cfg )
389+ gs .fieldContexts [field .data .qualifiedName ].GeneratedField .SetConfig (cfg )
378390 }
379391 }
380392}
381393
382394func (gs * DStructGeneratedStruct [T ]) propagateSettings (
383- node * Node [structField ],
395+ node * Node [StructField ],
384396 settings config.GenerationSettings ,
385397) {
386398 for _ , field := range node .children {
387- if field .HasChildren () &&
388- ! gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .IsCustomType () {
399+ if field .HasChildren () && gs .customTypes [field .data .goType ] == nil {
389400 gs .propagateSettings (field , settings )
390401 } else {
391- gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .SetGenerationSettings (settings )
402+ gs .fieldContexts [field .data .qualifiedName ].GeneratedField .SetGenerationSettings (settings )
392403 }
393404 }
394405}
395406
396407func (gs * DStructGeneratedStruct [T ]) propagateGenerationFunctions (
397- node * Node [structField ],
408+ node * Node [StructField ],
398409 functions core.DefaultGenerationFunctions ,
399410) {
400411 for _ , field := range node .children {
401412 if field .HasChildren () &&
402- ! gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .IsCustomType () {
413+ ! gs .fieldContexts [field .data .qualifiedName ].GeneratedField .IsCustomType () {
403414 gs .propagateGenerationFunctions (field , functions )
404415 } else {
405- gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .SetGenerationFunctions (functions )
416+ gs .fieldContexts [field .data .qualifiedName ].GeneratedField .SetGenerationFunctions (functions )
406417 }
407418 }
408419}
409420
410421func (gs * DStructGeneratedStruct [T ]) propagateGenerationFunction (
411- node * Node [structField ],
422+ node * Node [StructField ],
412423 functionHolder core.FunctionHolder ,
413424) {
414425 for _ , field := range node .children {
415426 if field .HasChildren () &&
416- ! gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .IsCustomType () {
427+ ! gs .fieldContexts [field .data .qualifiedName ].GeneratedField .IsCustomType () {
417428 gs .propagateGenerationFunction (field , functionHolder )
418429 } else {
419- gs .fieldContexts [field .data .fullyQualifiedName ].GeneratedField .SetGenerationFunction (functionHolder )
430+ gs .fieldContexts [field .data .qualifiedName ].GeneratedField .SetGenerationFunction (functionHolder )
420431 }
421432 }
422433}
0 commit comments