Skip to content

Commit d6c212b

Browse files
committed
rename creatonly tag to omitjsonupdate
1 parent 2a5577f commit d6c212b

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

internal/common/autogeneration/marshal.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import (
44
"encoding/json"
55
"fmt"
66
"reflect"
7-
"strings"
87

98
"github.com/hashicorp/terraform-plugin-framework/attr"
109
"github.com/hashicorp/terraform-plugin-framework/types"
1110
"github.com/huandu/xstrings"
1211
)
1312

1413
const (
15-
tagKey = "autogeneration"
16-
tagValOmitJSON = "omitjson"
17-
tagValCreateOnly = "createonly"
14+
tagKey = "autogeneration"
15+
tagValOmitJSON = "omitjson"
16+
tagValOmitJSONUpdate = "omitjsonupdate"
1817
)
1918

2019
// Marshal gets a Terraform model and marshals it into JSON (e.g. for an Atlas request).
21-
func Marshal(model any, isCreate bool) ([]byte, error) {
20+
func Marshal(model any, isUpdate bool) ([]byte, error) {
2221
valModel := reflect.ValueOf(model)
2322
if valModel.Kind() != reflect.Ptr {
2423
panic("model must be pointer")
@@ -27,7 +26,7 @@ func Marshal(model any, isCreate bool) ([]byte, error) {
2726
if valModel.Kind() != reflect.Struct {
2827
panic("model must be pointer to struct")
2928
}
30-
objJSON, err := marshalAttrs(valModel, isCreate)
29+
objJSON, err := marshalAttrs(valModel, isUpdate)
3130
if err != nil {
3231
return nil, err
3332
}
@@ -44,16 +43,16 @@ func Unmarshal(raw []byte, model any) error {
4443
return unmarshalAttrs(objJSON, model)
4544
}
4645

47-
func marshalAttrs(valModel reflect.Value, isCreate bool) (map[string]any, error) {
46+
func marshalAttrs(valModel reflect.Value, isUpdate bool) (map[string]any, error) {
4847
objJSON := make(map[string]any)
4948
for i := 0; i < valModel.NumField(); i++ {
5049
attrTypeModel := valModel.Type().Field(i)
5150
tag := attrTypeModel.Tag.Get(tagKey)
52-
if strings.Contains(tag, tagValOmitJSON) {
51+
if tag == tagValOmitJSON {
5352
continue // skip fields with tag `omitjson`
5453
}
55-
if !isCreate && strings.Contains(tag, tagValCreateOnly) {
56-
continue // skip fields with tag `createonly` if not in create
54+
if isUpdate && tag == tagValOmitJSONUpdate {
55+
continue // skip fields with tag `omitjsonupdate` if in update mode
5756
}
5857
attrNameModel := attrTypeModel.Name
5958
attrValModel := valModel.Field(i)

internal/common/autogeneration/marshal_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@ func TestMarshalBasic(t *testing.T) {
3535
assert.JSONEq(t, expectedJSON, string(raw))
3636
}
3737

38-
func TestMarshalCreateOnly(t *testing.T) {
38+
func TestMarshalOmitJSONUpdate(t *testing.T) {
3939
const (
40-
expectedCreate = `{ "attr": "val1", "attr_create_only": "val2" }`
41-
expectedNoCreate = `{ "attr": "val1" }`
40+
expectedCreate = `{ "attr": "val1", "attr_omit_update": "val2" }`
41+
expectedUpdate = `{ "attr": "val1" }`
4242
)
4343
model := struct {
4444
Attr types.String `tfsdk:"attr"`
45-
AttrCreateOnly types.String `tfsdk:"attr_create_only" autogeneration:"createonly"`
45+
AttrOmitUpdate types.String `tfsdk:"attr_create_only" autogeneration:"omitjsonupdate"`
4646
AttrOmit types.String `tfsdk:"attr_omit" autogeneration:"omitjson"`
4747
}{
4848
Attr: types.StringValue("val1"),
49-
AttrCreateOnly: types.StringValue("val2"),
49+
AttrOmitUpdate: types.StringValue("val2"),
5050
AttrOmit: types.StringValue("omit"),
5151
}
52-
noCreate, errNoCreate := autogeneration.Marshal(&model, false)
53-
require.NoError(t, errNoCreate)
54-
assert.JSONEq(t, expectedNoCreate, string(noCreate))
55-
56-
create, errCreate := autogeneration.Marshal(&model, true)
52+
create, errCreate := autogeneration.Marshal(&model, false)
5753
require.NoError(t, errCreate)
5854
assert.JSONEq(t, expectedCreate, string(create))
55+
56+
update, errUpdate := autogeneration.Marshal(&model, true)
57+
require.NoError(t, errUpdate)
58+
assert.JSONEq(t, expectedUpdate, string(update))
5959
}
6060

6161
func TestMarshalUnsupported(t *testing.T) {

0 commit comments

Comments
 (0)