Skip to content

Commit 37a5b2a

Browse files
authored
Replace all json.Marshal calls with utils.GoObjectToTerraformString (#244)
1 parent c509ac6 commit 37a5b2a

15 files changed

+73
-86
lines changed

port/action-permissions/refreshActionPermissionsToState.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package action_permissions
22

33
import (
4-
"encoding/json"
54
"github.com/hashicorp/terraform-plugin-framework/types"
65
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
76
"github.com/port-labs/terraform-provider-port-labs/v2/internal/flex"
7+
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
88
)
99

10-
func refreshActionPermissionsState(state *ActionPermissionsModel, a *cli.ActionPermissions, actionId string) error {
10+
func (r *ActionPermissionsResource) refreshActionPermissionsState(state *ActionPermissionsModel, a *cli.ActionPermissions, actionId string) error {
1111
state.ID = types.StringValue(actionId)
1212
state.ActionIdentifier = types.StringValue(actionId)
1313
state.BlueprintIdentifier = types.StringNull()
@@ -33,12 +33,12 @@ func refreshActionPermissionsState(state *ActionPermissionsModel, a *cli.ActionP
3333
state.Permissions.Execute.OwnedByTeam = flex.GoBoolToFramework(a.Execute.OwnedByTeam)
3434

3535
if a.Execute.Policy != nil {
36-
policy, err := json.Marshal(a.Execute.Policy)
36+
policy, err := utils.GoObjectToTerraformString(a.Execute.Policy, r.portClient.JSONEscapeHTML)
3737
if err != nil {
3838
return err
3939
}
4040

41-
state.Permissions.Execute.Policy = types.StringValue(string(policy))
41+
state.Permissions.Execute.Policy = policy
4242
}
4343

4444
state.Permissions.Approve = &ApproveModel{}
@@ -59,12 +59,12 @@ func refreshActionPermissionsState(state *ActionPermissionsModel, a *cli.ActionP
5959
}
6060

6161
if a.Approve.Policy != nil {
62-
policy, err := json.Marshal(a.Approve.Policy)
62+
policy, err := utils.GoObjectToTerraformString(a.Approve.Policy, r.portClient.JSONEscapeHTML)
6363
if err != nil {
6464
return err
6565
}
6666

67-
state.Permissions.Approve.Policy = types.StringValue(string(policy))
67+
state.Permissions.Approve.Policy = policy
6868
}
6969

7070
return nil

port/action-permissions/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (r *ActionPermissionsResource) Read(ctx context.Context, req resource.ReadR
6262
return
6363
}
6464

65-
err = refreshActionPermissionsState(state, a, actionIdentifier)
65+
err = r.refreshActionPermissionsState(state, a, actionIdentifier)
6666
if err != nil {
6767
resp.Diagnostics.AddError("failed to refresh action permissions state", err.Error())
6868
return

port/action/array.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package action
22

33
import (
44
"context"
5-
"encoding/json"
65
"reflect"
76

87
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -331,12 +330,11 @@ func (r *ActionResource) addArrayPropertiesToResource(v *cli.ActionProperty) (*A
331330
objectArray := make([]map[string]interface{}, len(v.Default.([]interface{})))
332331
attrs := make([]attr.Value, 0, len(objectArray))
333332
for _, value := range v.Default.([]interface{}) {
334-
stringfiyValue, err := json.Marshal(value)
333+
stringValue, err := utils.GoObjectToTerraformString(value, r.portClient.JSONEscapeHTML)
335334
if err != nil {
336335
return nil, err
337336
}
338-
stringValue := string(stringfiyValue)
339-
attrs = append(attrs, basetypes.NewStringValue(stringValue))
337+
attrs = append(attrs, stringValue)
340338
}
341339
arrayProp.ObjectItems.Default, _ = types.ListValue(types.StringType, attrs)
342340
}

port/action/refreshActionState.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package action
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"reflect"
87
"strconv"
@@ -362,11 +361,11 @@ func (r *ActionResource) writeTriggerToResource(ctx context.Context, a *cli.Acti
362361
}
363362

364363
if a.Trigger.Condition != nil {
365-
triggerCondition, err := json.Marshal(a.Trigger.Condition)
364+
triggerCondition, err := utils.GoObjectToTerraformString(a.Trigger.Condition, r.portClient.JSONEscapeHTML)
366365
if err != nil {
367366
return err
368367
}
369-
state.SelfServiceTrigger.Condition = types.StringValue(string(triggerCondition))
368+
state.SelfServiceTrigger.Condition = triggerCondition
370369
}
371370
}
372371

port/blueprint/array.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package blueprint
22

33
import (
44
"context"
5-
"encoding/json"
6-
75
"github.com/hashicorp/terraform-plugin-framework/attr"
86
"github.com/hashicorp/terraform-plugin-framework/types"
97
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
@@ -114,7 +112,7 @@ func arrayPropResourceToBody(ctx context.Context, state *PropertiesModel, props
114112
return nil
115113
}
116114

117-
func AddArrayPropertiesToState(v *cli.BlueprintProperty) *ArrayPropModel {
115+
func AddArrayPropertiesToState(v *cli.BlueprintProperty, jsonEscapeHTML bool) *ArrayPropModel {
118116
arrayProp := &ArrayPropModel{
119117
MinItems: flex.GoInt64ToFramework(v.MinItems),
120118
MaxItems: flex.GoInt64ToFramework(v.MaxItems),
@@ -179,9 +177,8 @@ func AddArrayPropertiesToState(v *cli.BlueprintProperty) *ArrayPropModel {
179177
}
180178
attrs := make([]attr.Value, 0, len(objectArray))
181179
for _, value := range objectArray {
182-
js, _ := json.Marshal(&value)
183-
stringValue := string(js)
184-
attrs = append(attrs, basetypes.NewStringValue(stringValue))
180+
stringValue, _ := utils.GoObjectToTerraformString(&value, jsonEscapeHTML)
181+
attrs = append(attrs, stringValue)
185182
}
186183
arrayProp.ObjectItems.Default, _ = types.ListValue(types.StringType, attrs)
187184
} else {

port/blueprint/resource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ func (r *BlueprintResource) Read(ctx context.Context, req resource.ReadRequest,
5757
return
5858
}
5959

60-
err = refreshBlueprintState(ctx, state, b)
60+
err = r.refreshBlueprintState(ctx, state, b)
6161
if err != nil {
6262
resp.Diagnostics.AddError("failed writing blueprint fields to resource", err.Error())
6363
return
6464
}
6565
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
6666
}
6767

68-
func refreshBlueprintState(ctx context.Context, bm *BlueprintModel, b *cli.Blueprint) error {
68+
func (r *BlueprintResource) refreshBlueprintState(ctx context.Context, bm *BlueprintModel, b *cli.Blueprint) error {
6969
bm.Identifier = types.StringValue(b.Identifier)
7070
bm.ID = types.StringValue(b.Identifier)
7171
bm.CreatedAt = types.StringValue(b.CreatedAt.String())
@@ -120,7 +120,7 @@ func refreshBlueprintState(ctx context.Context, bm *BlueprintModel, b *cli.Bluep
120120
}
121121

122122
if len(b.Schema.Properties) > 0 {
123-
err := updatePropertiesToState(ctx, b, bm)
123+
err := r.updatePropertiesToState(ctx, b, bm)
124124
if err != nil {
125125
return err
126126
}

port/blueprint/updatePropertiesToState.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package blueprint
22

33
import (
44
"context"
5-
"encoding/json"
5+
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
66

77
"github.com/hashicorp/terraform-plugin-framework/types"
88
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
99
"github.com/port-labs/terraform-provider-port-labs/v2/internal/flex"
1010
"github.com/samber/lo"
1111
)
1212

13-
func SetCommonProperties(v cli.BlueprintProperty, prop interface{}) {
13+
func SetCommonProperties(v cli.BlueprintProperty, prop interface{}, jsonEscapeHTML bool) {
1414
properties := []string{"Description", "Icon", "Default", "Title"}
1515
for _, property := range properties {
1616
switch property {
@@ -63,16 +63,14 @@ func SetCommonProperties(v cli.BlueprintProperty, prop interface{}) {
6363
case *BooleanPropModel:
6464
p.Default = types.BoolValue(v.Default.(bool))
6565
case *ObjectPropModel:
66-
js, _ := json.Marshal(v.Default)
67-
value := string(js)
68-
p.Default = types.StringValue(value)
66+
p.Default, _ = utils.GoObjectToTerraformString(v.Default, jsonEscapeHTML)
6967
}
7068
}
7169
}
7270
}
7371
}
7472

75-
func updatePropertiesToState(ctx context.Context, b *cli.Blueprint, bm *BlueprintModel) error {
73+
func (r *BlueprintResource) updatePropertiesToState(ctx context.Context, b *cli.Blueprint, bm *BlueprintModel) error {
7674
properties := &PropertiesModel{}
7775

7876
for k, v := range b.Schema.Properties {
@@ -89,7 +87,7 @@ func updatePropertiesToState(ctx context.Context, b *cli.Blueprint, bm *Blueprin
8987
stringProp.Required = types.BoolValue(false)
9088
}
9189

92-
SetCommonProperties(v, stringProp)
90+
SetCommonProperties(v, stringProp, r.portClient.JSONEscapeHTML)
9391

9492
properties.StringProps[k] = *stringProp
9593

@@ -106,7 +104,7 @@ func updatePropertiesToState(ctx context.Context, b *cli.Blueprint, bm *Blueprin
106104
numberProp.Required = types.BoolValue(false)
107105
}
108106

109-
SetCommonProperties(v, numberProp)
107+
SetCommonProperties(v, numberProp, r.portClient.JSONEscapeHTML)
110108

111109
properties.NumberProps[k] = *numberProp
112110

@@ -115,15 +113,15 @@ func updatePropertiesToState(ctx context.Context, b *cli.Blueprint, bm *Blueprin
115113
properties.ArrayProps = make(map[string]ArrayPropModel)
116114
}
117115

118-
arrayProp := AddArrayPropertiesToState(&v)
116+
arrayProp := AddArrayPropertiesToState(&v, r.portClient.JSONEscapeHTML)
119117

120118
if lo.Contains(b.Schema.Required, k) {
121119
arrayProp.Required = types.BoolValue(true)
122120
} else {
123121
arrayProp.Required = types.BoolValue(false)
124122
}
125123

126-
SetCommonProperties(v, arrayProp)
124+
SetCommonProperties(v, arrayProp, r.portClient.JSONEscapeHTML)
127125

128126
properties.ArrayProps[k] = *arrayProp
129127

@@ -134,7 +132,7 @@ func updatePropertiesToState(ctx context.Context, b *cli.Blueprint, bm *Blueprin
134132

135133
booleanProp := &BooleanPropModel{}
136134

137-
SetCommonProperties(v, booleanProp)
135+
SetCommonProperties(v, booleanProp, r.portClient.JSONEscapeHTML)
138136

139137
if lo.Contains(b.Schema.Required, k) {
140138
booleanProp.Required = types.BoolValue(true)
@@ -157,7 +155,7 @@ func updatePropertiesToState(ctx context.Context, b *cli.Blueprint, bm *Blueprin
157155
objectProp.Required = types.BoolValue(false)
158156
}
159157

160-
SetCommonProperties(v, objectProp)
158+
SetCommonProperties(v, objectProp, r.portClient.JSONEscapeHTML)
161159

162160
properties.ObjectProps[k] = *objectProp
163161
}

port/entity/refreshEntityToState.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package entity
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"github.com/hashicorp/terraform-plugin-framework/types"
87
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
8+
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
99
)
1010

11-
func refreshArrayEntityState(ctx context.Context, state *EntityModel, arrayProperties map[string][]interface{}, blueprint *cli.Blueprint) {
11+
func (r *EntityResource) refreshArrayEntityState(ctx context.Context, state *EntityModel, arrayProperties map[string][]interface{}, blueprint *cli.Blueprint) {
1212
mapStringItems := make(map[string][]*string)
1313
mapNumberItems := make(map[string][]*float64)
1414
mapBooleanItems := make(map[string][]*bool)
@@ -70,9 +70,8 @@ func refreshArrayEntityState(ctx context.Context, state *EntityModel, arrayPrope
7070
case "object":
7171
if t != nil {
7272
for _, item := range t {
73-
js, _ := json.Marshal(&item)
74-
stringJs := string(js)
75-
mapObjectItems[k] = append(mapObjectItems[k], &stringJs)
73+
stringJs, _ := utils.GoObjectToTerraformString(&item, r.portClient.JSONEscapeHTML)
74+
mapObjectItems[k] = append(mapObjectItems[k], stringJs.ValueStringPointer())
7675
}
7776
if len(t) == 0 {
7877
mapObjectItems[k] = []*string{}
@@ -85,7 +84,7 @@ func refreshArrayEntityState(ctx context.Context, state *EntityModel, arrayPrope
8584
}
8685
}
8786

88-
func refreshPropertiesEntityState(ctx context.Context, state *EntityModel, e *cli.Entity, blueprint *cli.Blueprint) {
87+
func (r *EntityResource) refreshPropertiesEntityState(ctx context.Context, state *EntityModel, e *cli.Entity, blueprint *cli.Blueprint) {
8988
state.Properties = &EntityPropertiesModel{}
9089
arrayProperties := make(map[string][]interface{})
9190
for k, v := range e.Properties {
@@ -111,8 +110,7 @@ func refreshPropertiesEntityState(ctx context.Context, state *EntityModel, e *cl
111110
if state.Properties.ObjectProps == nil {
112111
state.Properties.ObjectProps = make(map[string]types.String)
113112
}
114-
js, _ := json.Marshal(&t)
115-
state.Properties.ObjectProps[k] = types.StringValue(string(js))
113+
state.Properties.ObjectProps[k], _ = utils.GoObjectToTerraformString(&t, r.portClient.JSONEscapeHTML)
116114
case nil:
117115
switch blueprint.Schema.Properties[k].Type {
118116
case "string":
@@ -141,7 +139,7 @@ func refreshPropertiesEntityState(ctx context.Context, state *EntityModel, e *cl
141139
}
142140
}
143141
if len(arrayProperties) != 0 {
144-
refreshArrayEntityState(ctx, state, arrayProperties, blueprint)
142+
r.refreshArrayEntityState(ctx, state, arrayProperties, blueprint)
145143
}
146144
}
147145

@@ -179,7 +177,7 @@ func refreshRelationsEntityState(ctx context.Context, state *EntityModel, e *cli
179177
}
180178
}
181179

182-
func refreshEntityState(ctx context.Context, state *EntityModel, e *cli.Entity, blueprint *cli.Blueprint) error {
180+
func (r *EntityResource) refreshEntityState(ctx context.Context, state *EntityModel, e *cli.Entity, blueprint *cli.Blueprint) error {
183181
state.ID = types.StringValue(fmt.Sprintf("%s:%s", blueprint.Identifier, e.Identifier))
184182
state.Identifier = types.StringValue(e.Identifier)
185183
state.Blueprint = types.StringValue(blueprint.Identifier)
@@ -204,7 +202,7 @@ func refreshEntityState(ctx context.Context, state *EntityModel, e *cli.Entity,
204202
}
205203

206204
if len(e.Properties) != 0 {
207-
refreshPropertiesEntityState(ctx, state, e, blueprint)
205+
r.refreshPropertiesEntityState(ctx, state, e, blueprint)
208206
}
209207

210208
if len(e.Relations) != 0 {

port/entity/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (r *EntityResource) Read(ctx context.Context, req resource.ReadRequest, res
6060
return
6161
}
6262

63-
err = refreshEntityState(ctx, state, e, b)
63+
err = r.refreshEntityState(ctx, state, e, b)
6464
if err != nil {
6565
resp.Diagnostics.AddError("failed writing entity fields to resource", err.Error())
6666
return

port/page/refreshPageToState.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package page
22

33
import (
4-
"encoding/json"
54
"github.com/hashicorp/terraform-plugin-framework/types"
65
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
6+
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
77
)
88

9-
func refreshPageToState(pm *PageModel, b *cli.Page) error {
9+
func (r *PageResource) refreshPageToState(pm *PageModel, b *cli.Page) error {
1010
pm.ID = types.StringValue(b.Identifier)
1111
pm.Identifier = types.StringValue(b.Identifier)
1212
pm.Type = types.StringValue(b.Type)
@@ -22,11 +22,11 @@ func refreshPageToState(pm *PageModel, b *cli.Page) error {
2222
if b.Widgets != nil {
2323
// go over each widget and convert it to a string and store it in the widgets array
2424
for i, widget := range *b.Widgets {
25-
bWidget, err := json.Marshal(widget)
25+
bWidget, err := utils.GoObjectToTerraformString(widget, r.portClient.JSONEscapeHTML)
2626
if err != nil {
2727
return err
2828
}
29-
pm.Widgets[i] = types.StringValue(string(bWidget))
29+
pm.Widgets[i] = bWidget
3030
}
3131
}
3232
return nil

port/page/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (r *PageResource) Read(ctx context.Context, req resource.ReadRequest, resp
6262
return
6363
}
6464

65-
err = refreshPageToState(state, p)
65+
err = r.refreshPageToState(state, p)
6666

6767
if err != nil {
6868
resp.Diagnostics.AddError("failed to write page fields to resource", err.Error())

port/search/dataSource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (d *SearchDataSource) Read(ctx context.Context, req datasource.ReadRequest,
6060

6161
for _, entity := range searchResult.Entities {
6262
matchingEntityBlueprint := blueprints[entity.Blueprint]
63-
e := refreshEntityState(ctx, &entity, &matchingEntityBlueprint)
63+
e := d.refreshEntityState(ctx, &entity, &matchingEntityBlueprint)
6464
data.Entities = append(data.Entities, *e)
6565
}
6666

0 commit comments

Comments
 (0)