@@ -191,7 +191,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, instance gjson.R
191191
192192 if ! attrs .Exists () {
193193 // Set schema_version even for invalid instances
194- result , _ = sjson . Set (result , "schema_version" , 0 )
194+ result = state . SetSchemaVersion (result , 0 )
195195 return result , nil
196196 }
197197
@@ -224,7 +224,7 @@ func (m *V4ToV5Migrator) TransformState(ctx *transform.Context, instance gjson.R
224224 // We should NOT remove them, just leave them as-is for provider to handle
225225
226226 // Set schema_version
227- result , _ = sjson . Set (result , "schema_version" , 0 )
227+ result = state . SetSchemaVersion (result , 0 )
228228
229229 return result , nil
230230}
@@ -263,15 +263,12 @@ func (m *V4ToV5Migrator) createHTTPConfig(stateJSON string, attrs gjson.Result)
263263 stateJSON , _ = sjson .Set (stateJSON , "attributes.http_config" , httpConfig )
264264
265265 // Remove the root-level fields that moved into http_config
266+ fieldNames := make ([]string , 0 , len (httpFields )+ 1 )
266267 for oldField := range httpFields {
267- if attrs .Get (oldField ).Exists () {
268- stateJSON , _ = sjson .Delete (stateJSON , "attributes." + oldField )
269- }
270- }
271- // Remove header from root if it exists
272- if attrs .Get ("header" ).Exists () {
273- stateJSON , _ = sjson .Delete (stateJSON , "attributes.header" )
268+ fieldNames = append (fieldNames , oldField )
274269 }
270+ fieldNames = append (fieldNames , "header" )
271+ stateJSON = state .RemoveFieldsIfExist (stateJSON , "attributes" , attrs , fieldNames ... )
275272 }
276273
277274 return stateJSON
@@ -294,12 +291,7 @@ func (m *V4ToV5Migrator) createTCPConfig(stateJSON string, attrs gjson.Result) s
294291 stateJSON , _ = sjson .Set (stateJSON , "attributes.tcp_config" , tcpConfig )
295292
296293 // Remove the root-level fields that moved into tcp_config
297- if attrs .Get ("method" ).Exists () {
298- stateJSON , _ = sjson .Delete (stateJSON , "attributes.method" )
299- }
300- if attrs .Get ("port" ).Exists () {
301- stateJSON , _ = sjson .Delete (stateJSON , "attributes.port" )
302- }
294+ stateJSON = state .RemoveFieldsIfExist (stateJSON , "attributes" , attrs , "method" , "port" )
303295 }
304296
305297 return stateJSON
0 commit comments