forked from snyk/driftctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deserializer.go
49 lines (40 loc) · 1.24 KB
/
deserializer.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
45
46
47
48
49
package resource
import (
"encoding/json"
"github.com/snyk/driftctl/enumeration/resource"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
type Deserializer struct {
factory resource.ResourceFactory
}
func NewDeserializer(factory resource.ResourceFactory) *Deserializer {
return &Deserializer{factory}
}
func (s *Deserializer) Deserialize(ty string, rawList []cty.Value) ([]*resource.Resource, error) {
resources := make([]*resource.Resource, 0)
for _, rawResource := range rawList {
rawResource := rawResource
res, err := s.DeserializeOne(ty, rawResource)
if err != nil {
return nil, err
}
resources = append(resources, res)
}
return resources, nil
}
func (s *Deserializer) DeserializeOne(ty string, value cty.Value) (*resource.Resource, error) {
if value.IsNull() {
return nil, nil
}
// Marked values cannot be deserialized to JSON.
// For example, this ensures we can deserialize sensitive values too.
unmarkedVal, _ := value.UnmarkDeep()
var attrs resource.Attributes
bytes, _ := ctyjson.Marshal(unmarkedVal, unmarkedVal.Type())
err := json.Unmarshal(bytes, &attrs)
if err != nil {
return nil, err
}
return s.factory.CreateAbstractResource(ty, value.GetAttr("id").AsString(), attrs), nil
}