@@ -10,68 +10,68 @@ import (
10
10
"strings"
11
11
)
12
12
13
- func buildModelField (modelJSON * BlockToStruct , schemaData interface {}, mapValue interface {}, arrIndexer * ArrIndexer ) {
13
+ func ( converter * TFSchemaModelConverter [ T ]) buildModelField (modelJSON * BlockToStruct , schemaData interface {}, mapValue interface {}, arrIndexer * ArrIndexer ) {
14
14
if schemaData == nil || mapValue == nil {
15
15
return
16
16
}
17
17
18
18
switch mapValue := mapValue .(type ) {
19
19
case * BlockToStruct :
20
- modelHandleBlockStruct (modelJSON , schemaData , mapValue , arrIndexer )
20
+ converter . modelHandleBlockStruct (modelJSON , schemaData , mapValue , arrIndexer )
21
21
case * Map :
22
- modelHandleBlockMap (modelJSON , schemaData , mapValue , arrIndexer )
22
+ converter . modelHandleBlockMap (modelJSON , schemaData , mapValue , arrIndexer )
23
23
case * BlockToStructSlice :
24
- modelHandleBlockStructSlice (modelJSON , schemaData , mapValue , arrIndexer )
24
+ converter . modelHandleBlockStructSlice (modelJSON , schemaData , mapValue , arrIndexer )
25
25
case * BlockSliceToStructSlice :
26
- modelHandleBlockSliceStructSlice (modelJSON , schemaData , mapValue , arrIndexer )
26
+ converter . modelHandleBlockSliceStructSlice (modelJSON , schemaData , mapValue , arrIndexer )
27
27
case * ListToStruct :
28
- modelHandleListStruct (modelJSON , schemaData , mapValue , arrIndexer )
28
+ converter . modelHandleListStruct (modelJSON , schemaData , mapValue , arrIndexer )
29
29
case * EvaluatedField :
30
30
modelField := mapValue .Field
31
31
modelValue := mapValue .EvalFunc (ConstructModel , schemaData )
32
- setModelValue (modelJSON , modelField , modelValue , arrIndexer )
32
+ converter . setModelValue (modelJSON , modelField , modelValue , arrIndexer )
33
33
case string :
34
34
modelField := mapValue
35
35
modelValue := schemaData
36
- setModelValue (modelJSON , modelField , modelValue , arrIndexer )
36
+ converter . setModelValue (modelJSON , modelField , modelValue , arrIndexer )
37
37
}
38
38
}
39
39
40
- func modelHandleBlockStruct (modelJSON * BlockToStruct , schemaData interface {}, mapValue * BlockToStruct , arrIndexer * ArrIndexer ) {
40
+ func ( converter * TFSchemaModelConverter [ T ]) modelHandleBlockStruct (modelJSON * BlockToStruct , schemaData interface {}, mapValue * BlockToStruct , arrIndexer * ArrIndexer ) {
41
41
if schemaDataSlice , ok := schemaData .([]interface {}); ok && len (schemaDataSlice ) > 0 {
42
42
rootSchemaDict , _ := schemaDataSlice [0 ].(map [string ]interface {})
43
43
44
44
for key , value := range * mapValue {
45
- buildModelField (modelJSON , rootSchemaDict [key ], value , arrIndexer )
45
+ converter . buildModelField (modelJSON , rootSchemaDict [key ], value , arrIndexer )
46
46
}
47
47
}
48
48
}
49
49
50
- func modelHandleBlockMap (modelJSON * BlockToStruct , schemaData interface {}, mapValue * Map , arrIndexer * ArrIndexer ) {
50
+ func ( converter * TFSchemaModelConverter [ T ]) modelHandleBlockMap (modelJSON * BlockToStruct , schemaData interface {}, mapValue * Map , arrIndexer * ArrIndexer ) {
51
51
if rootSchemaDict , ok := schemaData .(map [string ]interface {}); ok {
52
- definedKeysMapValue := mapValue .Copy ([]string {"*" })
52
+ definedKeysMapValue := mapValue .Copy ([]string {AllMapKeysFieldMarker })
53
53
54
- if allKeysFlagMapValue , exists := (* mapValue )["*" ]; exists {
54
+ if allKeysFlagMapValue , exists := (* mapValue )[AllMapKeysFieldMarker ]; exists {
55
55
for key , value := range rootSchemaDict {
56
56
var dynamicMapValue interface {}
57
57
58
58
if allKeysFlagMapStr , ok := allKeysFlagMapValue .(string ); ok {
59
- dynamicMapValue = strings .ReplaceAll (allKeysFlagMapStr , "*" , key )
59
+ dynamicMapValue = strings .ReplaceAll (allKeysFlagMapStr , AllMapKeysFieldMarker , key )
60
60
} else {
61
61
dynamicMapValue = allKeysFlagMapValue
62
62
}
63
63
64
- buildModelField (modelJSON , value , dynamicMapValue , arrIndexer )
64
+ converter . buildModelField (modelJSON , value , dynamicMapValue , arrIndexer )
65
65
}
66
66
}
67
67
68
68
for key , value := range definedKeysMapValue {
69
- buildModelField (modelJSON , rootSchemaDict [key ], value , arrIndexer )
69
+ converter . buildModelField (modelJSON , rootSchemaDict [key ], value , arrIndexer )
70
70
}
71
71
}
72
72
}
73
73
74
- func modelHandleBlockStructSlice (modelJSON * BlockToStruct , schemaData interface {}, mapValue * BlockToStructSlice , arrIndexer * ArrIndexer ) {
74
+ func ( converter * TFSchemaModelConverter [ T ]) modelHandleBlockStructSlice (modelJSON * BlockToStruct , schemaData interface {}, mapValue * BlockToStructSlice , arrIndexer * ArrIndexer ) {
75
75
if len (schemaData .([]interface {})) > 0 {
76
76
arrIndexer .New ()
77
77
@@ -81,10 +81,10 @@ func modelHandleBlockStructSlice(modelJSON *BlockToStruct, schemaData interface{
81
81
82
82
if schemaValue != nil {
83
83
if _ , ok := elemMapValue .(* ListToStruct ); ok {
84
- buildModelField (modelJSON , schemaValue , elemMapValue , arrIndexer )
84
+ converter . buildModelField (modelJSON , schemaValue , elemMapValue , arrIndexer )
85
85
} else {
86
86
for _ , item := range schemaValue .([]interface {}) {
87
- buildModelField (modelJSON , []interface {}{item }, elemMapValue , arrIndexer )
87
+ converter . buildModelField (modelJSON , []interface {}{item }, elemMapValue , arrIndexer )
88
88
arrIndexer .IncrementLastIndex ()
89
89
}
90
90
}
@@ -96,7 +96,7 @@ func modelHandleBlockStructSlice(modelJSON *BlockToStruct, schemaData interface{
96
96
}
97
97
}
98
98
99
- func modelHandleBlockSliceStructSlice (modelJSON * BlockToStruct , schemaData interface {}, mapValue * BlockSliceToStructSlice , arrIndexer * ArrIndexer ) {
99
+ func ( converter * TFSchemaModelConverter [ T ]) modelHandleBlockSliceStructSlice (modelJSON * BlockToStruct , schemaData interface {}, mapValue * BlockSliceToStructSlice , arrIndexer * ArrIndexer ) {
100
100
if len (schemaData .([]interface {})) > 0 {
101
101
for _ , elemTypeMap := range * mapValue {
102
102
arrIndexer .New ()
@@ -105,7 +105,7 @@ func modelHandleBlockSliceStructSlice(modelJSON *BlockToStruct, schemaData inter
105
105
var _ , ok = item .(map [string ]interface {})
106
106
107
107
if ok {
108
- buildModelField (modelJSON , []interface {}{item }, elemTypeMap , arrIndexer )
108
+ converter . buildModelField (modelJSON , []interface {}{item }, elemTypeMap , arrIndexer )
109
109
arrIndexer .IncrementLastIndex ()
110
110
}
111
111
}
@@ -115,32 +115,34 @@ func modelHandleBlockSliceStructSlice(modelJSON *BlockToStruct, schemaData inter
115
115
}
116
116
}
117
117
118
- func modelHandleListStruct (modelJSON * BlockToStruct , schemaData interface {}, mapValue * ListToStruct , arrIndexer * ArrIndexer ) {
118
+ func ( converter * TFSchemaModelConverter [ T ]) modelHandleListStruct (modelJSON * BlockToStruct , schemaData interface {}, mapValue * ListToStruct , arrIndexer * ArrIndexer ) {
119
119
if reflect .TypeOf (schemaData ).Kind () == reflect .Slice {
120
120
sliceValue := reflect .ValueOf (schemaData )
121
121
122
122
for i := 0 ; i < sliceValue .Len (); i ++ {
123
123
val := sliceValue .Index (i ).Interface ()
124
- setModelValue (modelJSON , (* mapValue )[0 ], val , arrIndexer )
124
+ converter . setModelValue (modelJSON , (* mapValue )[0 ], val , arrIndexer )
125
125
arrIndexer .IncrementLastIndex ()
126
126
}
127
127
}
128
128
}
129
129
130
- func setModelValue (model * BlockToStruct , field string , value interface {}, arrIndexer * ArrIndexer ) {
131
- if ! strings .Contains (field , "." ) {
130
+ func (converter * TFSchemaModelConverter [T ]) setModelValue (model * BlockToStruct , field string , value interface {}, arrIndexer * ArrIndexer ) {
131
+ modelPathSeparator := converter .getModelPathSeparator ()
132
+
133
+ if ! strings .Contains (field , modelPathSeparator ) {
132
134
(* model )[field ] = value
133
135
} else {
134
- fieldPaths := strings .Split (field , "." )
136
+ fieldPaths := strings .Split (field , modelPathSeparator )
135
137
arrIndices := arrIndexer .GetAllIndexes ()
136
- leafField := strings .ReplaceAll (fieldPaths [len (fieldPaths )- 1 ], "[]" , "" )
138
+ leafField := strings .ReplaceAll (fieldPaths [len (fieldPaths )- 1 ], ArrayFieldMarker , "" )
137
139
arrayFields := 0
138
140
parentField := * model
139
141
140
142
for i := 0 ; i < len (fieldPaths )- 1 ; i ++ {
141
143
fieldName := fieldPaths [i ]
142
144
143
- if ! strings .Contains (fieldName , "[]" ) {
145
+ if ! strings .Contains (fieldName , ArrayFieldMarker ) {
144
146
if _ , ok := parentField [fieldName ]; ! ok {
145
147
parentField [fieldName ] = make (map [string ]interface {})
146
148
}
@@ -149,7 +151,7 @@ func setModelValue(model *BlockToStruct, field string, value interface{}, arrInd
149
151
} else {
150
152
var object map [string ]interface {}
151
153
152
- fieldName = strings .ReplaceAll (fieldName , "[]" , "" )
154
+ fieldName = strings .ReplaceAll (fieldName , ArrayFieldMarker , "" )
153
155
arrayIndex := arrIndices [arrayFields ]
154
156
155
157
arrayFields ++
0 commit comments