-
Notifications
You must be signed in to change notification settings - Fork 85
/
canonical.go
44 lines (39 loc) · 1.19 KB
/
canonical.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0
package config
import (
"github.com/pkg/errors"
"github.com/crossplane/upjet/pkg/resource/json"
)
const (
errFmtNotJSONString = "parameter at path %q with value %v is not a (JSON) string"
errFmtCanonicalize = "failed to canonicalize the parameter at path %q"
)
// CanonicalizeJSONParameters returns a ConfigurationInjector that computes
// and stores the canonical forms of the JSON documents for the specified list
// of top-level Terraform configuration arguments. Please note that currently
// only top-level configuration arguments are supported by this function.
func CanonicalizeJSONParameters(tfPath ...string) ConfigurationInjector {
return func(jsonMap map[string]any, tfMap map[string]any) error {
for _, param := range tfPath {
p, ok := tfMap[param]
if !ok {
continue
}
s, ok := p.(string)
if !ok {
return errors.Errorf(errFmtNotJSONString, param, p)
}
if s == "" {
continue
}
cJSON, err := json.Canonicalize(s)
if err != nil {
return errors.Wrapf(err, errFmtCanonicalize, param)
}
tfMap[param] = cJSON
}
return nil
}
}