pulumi/pkg/pack/encoding/decode.go
joeduffy 1948380eb2 Add custom decoders to eliminate boilerplate
This change overhauls the approach to custom decoding.  Instead of decoding
the parts of the struct that are "trivial" in one pass, and then patching up
the structure afterwards with custom decoding, the decoder itself understands
the notion of custom decoder functions.

First, the general purpose logic has moved out of pkg/pack/encoding and into
a new package, pkg/util/mapper.  Most functions are now members of a new top-
level type, Mapper, which may be initialized with custom decoders.  This
is a map from target type to a function that can decode objects into it.

Second, the AST-specific decoding logic is rewritten to use it.  All AST nodes
are now supported, including definitions, statements, and expressions.  The
overall approach here is to simply define a custom decoder for any interface
type that will occur in a node field position.  The mapper, upon encountering
such a type, will consult the custom decoder map; if a decoder is found, it
will be used, otherwise an error results.  This decoder then needs to switch
on the type discriminated kind field that is present in the metadata, creating
a concrete struct of the right type, and then converting it to the desired
interface type.  Note that, subtly, interface types used only for "marker"
purposes don't require any custom decoding, because they do not appear in
field positions and therefore won't be encountered during the decoding process.
2017-01-16 09:41:26 -08:00

62 lines
2.7 KiB
Go

// Copyright 2016 Marapongo, Inc. All rights reserved.
// Because of the complex structure of the MuPack and MuIL metadata formats, we cannot rely on the standard JSON
// marshaling and unmarshaling routines. Instead, we will need to do it mostly "by hand". This package does that.
package encoding
import (
"reflect"
"github.com/marapongo/mu/pkg/encoding"
"github.com/marapongo/mu/pkg/pack"
"github.com/marapongo/mu/pkg/pack/ast"
"github.com/marapongo/mu/pkg/util/mapper"
)
// Decode unmarshals the entire contents of the given byte array into a Package object.
func Decode(m encoding.Marshaler, b []byte) (*pack.Package, error) {
// First convert the whole contents of the metadata into a map. Although it would be more efficient to walk the
// token stream, token by token, this allows us to reuse existing YAML packages in addition to JSON ones.
var tree mapper.Object
if err := m.Unmarshal(b, &tree); err != nil {
return nil, err
}
// Now decode the top-level Package metadata; this will automatically recurse throughout the whole structure.
md := mapper.New(customDecoders())
var pack pack.Package
if err := md.Decode(tree, &pack); err != nil {
return nil, err
}
return &pack, nil
}
// customDecoders makes a complete map of all known custom AST decoders. In general, any polymorphic node kind that
// appears as a field in another concrete marshalable structure must have an associated custom decoder. If not, the
// Mapper will error out. This is typically an interface type and that method typically switches on the kind property.
// Note that interfaces that are used as "markers" and don't show up in fields are okay and don't require a decoder.
func customDecoders() mapper.Decoders {
return mapper.Decoders{
reflect.TypeOf((*ast.ModuleMember)(nil)).Elem(): moduleMemberDecoder,
reflect.TypeOf((*ast.ClassMember)(nil)).Elem(): classMemberDecoder,
reflect.TypeOf((*ast.Statement)(nil)).Elem(): statementDecoder,
reflect.TypeOf((*ast.Expression)(nil)).Elem(): expressionDecoder,
}
}
// Each of the custom decoders is a varaible that points to a decoder function; this is done so that the decode*
// functions can remain strongly typed, as the mapper's decoder signature requires a weakly-typed interface{} return.
var moduleMemberDecoder = func(m mapper.Mapper, tree mapper.Object) (interface{}, error) {
return decodeModuleMember(m, tree)
}
var classMemberDecoder = func(m mapper.Mapper, tree mapper.Object) (interface{}, error) {
return decodeClassMember(m, tree)
}
var statementDecoder = func(m mapper.Mapper, tree mapper.Object) (interface{}, error) {
return decodeStatement(m, tree)
}
var expressionDecoder = func(m mapper.Mapper, tree mapper.Object) (interface{}, error) {
return decodeExpression(m, tree)
}