@@ -4,21 +4,20 @@ import (
4
4
"encoding/json"
5
5
"fmt"
6
6
"reflect"
7
- "strings"
8
7
9
8
"github.com/hashicorp/terraform-plugin-framework/attr"
10
9
"github.com/hashicorp/terraform-plugin-framework/types"
11
10
"github.com/huandu/xstrings"
12
11
)
13
12
14
13
const (
15
- tagKey = "autogeneration"
16
- tagValOmitJSON = "omitjson"
17
- tagValCreateOnly = "createonly "
14
+ tagKey = "autogeneration"
15
+ tagValOmitJSON = "omitjson"
16
+ tagValOmitJSONUpdate = "omitjsonupdate "
18
17
)
19
18
20
19
// 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 ) {
22
21
valModel := reflect .ValueOf (model )
23
22
if valModel .Kind () != reflect .Ptr {
24
23
panic ("model must be pointer" )
@@ -27,7 +26,7 @@ func Marshal(model any, isCreate bool) ([]byte, error) {
27
26
if valModel .Kind () != reflect .Struct {
28
27
panic ("model must be pointer to struct" )
29
28
}
30
- objJSON , err := marshalAttrs (valModel , isCreate )
29
+ objJSON , err := marshalAttrs (valModel , isUpdate )
31
30
if err != nil {
32
31
return nil , err
33
32
}
@@ -44,16 +43,16 @@ func Unmarshal(raw []byte, model any) error {
44
43
return unmarshalAttrs (objJSON , model )
45
44
}
46
45
47
- func marshalAttrs (valModel reflect.Value , isCreate bool ) (map [string ]any , error ) {
46
+ func marshalAttrs (valModel reflect.Value , isUpdate bool ) (map [string ]any , error ) {
48
47
objJSON := make (map [string ]any )
49
48
for i := 0 ; i < valModel .NumField (); i ++ {
50
49
attrTypeModel := valModel .Type ().Field (i )
51
50
tag := attrTypeModel .Tag .Get (tagKey )
52
- if strings . Contains ( tag , tagValOmitJSON ) {
51
+ if tag == tagValOmitJSON {
53
52
continue // skip fields with tag `omitjson`
54
53
}
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
57
56
}
58
57
attrNameModel := attrTypeModel .Name
59
58
attrValModel := valModel .Field (i )
0 commit comments