2021-05-21 14:09:45 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2021-06-02 10:01:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2021-06-02 10:01:55 +00:00
|
|
|
ctyjson "github.com/zclconf/go-cty/cty/json"
|
2021-05-21 14:09:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Deserializer struct {
|
|
|
|
factory ResourceFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDeserializer(factory ResourceFactory) *Deserializer {
|
|
|
|
return &Deserializer{factory}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Deserializer) Deserialize(ty string, rawList []cty.Value) ([]Resource, error) {
|
|
|
|
resources := make([]Resource, 0)
|
|
|
|
for _, rawResource := range rawList {
|
|
|
|
rawResource := rawResource
|
2021-06-11 15:10:06 +00:00
|
|
|
res, err := s.DeserializeOne(ty, rawResource)
|
2021-06-02 10:01:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
resources = append(resources, res)
|
|
|
|
}
|
|
|
|
return resources, nil
|
|
|
|
}
|
2021-06-11 15:10:06 +00:00
|
|
|
|
|
|
|
func (s *Deserializer) DeserializeOne(ty string, value cty.Value) (Resource, error) {
|
2021-07-06 09:14:51 +00:00
|
|
|
if value.IsNull() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-06-11 15:10:06 +00:00
|
|
|
|
2021-07-06 09:14:51 +00:00
|
|
|
var attrs Attributes
|
2021-06-11 15:10:06 +00:00
|
|
|
bytes, _ := ctyjson.Marshal(value, value.Type())
|
|
|
|
err := json.Unmarshal(bytes, &attrs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.factory.CreateAbstractResource(ty, value.GetAttr("id").AsString(), attrs), nil
|
|
|
|
}
|