pulumi/pkg/encoding/marshal.go
joeduffy 2dd8665c46 Prepare for semantic analysis
This change begins to lay the groundwork for doing semantic analysis and
lowering to the cloud target's representation.  In particular:

* Split the mu/schema package.  There is now mu/ast which contains the
  core types and mu/encoding which concerns itself with JSON and YAML
  serialization.

* Notably I am *not* yet introducing a second AST form.  Instead, we will
  keep the parse tree and AST unified for the time being.  I envision very
  little difference between them -- at least for now -- and so this keeps
  things simpler, at the expense of two downsides: 1) the trees will be
  mutable (which turns out to be a good thing for performance), and 2) some
  fields will need to be ignored during de/serialization.  We can always
  revisit this later when and if the need to split them arises.

* Add a binder phase.  It is currently a no-op.
2016-11-16 09:29:44 -08:00

41 lines
898 B
Go

// Copyright 2016 Marapongo, Inc. All rights reserved.
package encoding
import (
"encoding/json"
"github.com/ghodss/yaml"
)
// Marshalers is a map of extension to a Marshaler object for that extension.
var Marshalers map[string]Marshaler
// Marshaler is a type that knows how to marshal and unmarshal data in one format.
type Marshaler interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}
type jsonMarshaler struct {
}
func (m *jsonMarshaler) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (m *jsonMarshaler) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
type yamlMarshaler struct {
}
func (m *yamlMarshaler) Marshal(v interface{}) ([]byte, error) {
return yaml.Marshal(v)
}
func (m *yamlMarshaler) Unmarshal(data []byte, v interface{}) error {
return yaml.Unmarshal(data, v)
}