forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmithy.go
38 lines (29 loc) · 888 Bytes
/
smithy.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package json
import (
smithydocument "github.com/aws/smithy-go/document"
)
func SmithyDocumentFromString[T smithydocument.Marshaler](s string, f func(any) T) (T, error) {
var v map[string]any
err := DecodeFromString(s, &v)
if err != nil {
var zero T
return zero, err
}
return f(v), nil
}
// SmithyDocumentToString converts a [Smithy document](https://smithy.io/2.0/spec/simple-types.html#document) to a JSON string.
func SmithyDocumentToString(document smithydocument.Unmarshaler) (string, error) {
var v map[string]any
err := document.UnmarshalSmithyDocument(&v)
if err != nil {
return "", err
}
return EncodeToString(v)
}
// JSONStringer interface is used to marshal and unmarshal JSON interface objects.
type JSONStringer interface {
smithydocument.Marshaler
smithydocument.Unmarshaler
}