2017-03-15 03:26:14 +01:00
|
|
|
// Copyright 2017 Pulumi, Inc. All rights reserved.
|
2017-01-14 16:40:13 +01:00
|
|
|
|
2017-03-10 22:27:19 +01:00
|
|
|
// Package encoding can unmarshal CocoPack and CocoIL metadata formats. Because of their complex structure, we cannot
|
2017-02-25 16:25:33 +01:00
|
|
|
// rely on the standard JSON marshaling and unmarshaling routines. Instead, we will need to do it mostly "by hand".
|
2017-01-14 16:40:13 +01:00
|
|
|
package encoding
|
|
|
|
|
|
|
|
import (
|
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 18:41:26 +01:00
|
|
|
"reflect"
|
|
|
|
|
2017-02-25 16:25:33 +01:00
|
|
|
"github.com/pulumi/coconut/pkg/compiler/ast"
|
|
|
|
"github.com/pulumi/coconut/pkg/pack"
|
|
|
|
"github.com/pulumi/coconut/pkg/util/mapper"
|
2017-01-14 16:40:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Decode unmarshals the entire contents of the given byte array into a Package object.
|
2017-01-17 23:58:45 +01:00
|
|
|
func Decode(m Marshaler, b []byte) (*pack.Package, error) {
|
2017-01-14 16:40:13 +01:00
|
|
|
// 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.
|
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 18:41:26 +01:00
|
|
|
var tree mapper.Object
|
2017-01-14 16:40:13 +01:00
|
|
|
if err := m.Unmarshal(b, &tree); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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 18:41:26 +01:00
|
|
|
// Now decode the top-level Package metadata; this will automatically recurse throughout the whole structure.
|
2017-03-12 22:13:44 +01:00
|
|
|
md := mapper.New(&mapper.Opts{CustomDecoders: customDecoders()})
|
2017-01-14 18:42:05 +01:00
|
|
|
var pack pack.Package
|
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 18:41:26 +01:00
|
|
|
if err := md.Decode(tree, &pack); err != nil {
|
2017-01-14 16:40:13 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
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 18:41:26 +01:00
|
|
|
return &pack, nil
|
|
|
|
}
|
2017-01-14 16:40:13 +01:00
|
|
|
|
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 18:41:26 +01:00
|
|
|
// 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,
|
Implement custom decoding of ModuleMembers
This change begins to implement some of the AST custom decoding, beneath
the Package's Module map. In particular, we now unmarshal "one level"
beyond this, populating each Module's ModuleMember map. This includes
Classes, Exports, ModuleProperties, and ModuleMethods. The Class AST's
Members have been marked "custom", in addition to Block's Statements,
because they required kind-directed decoding. But Exports and
ModuleProperties can be decoded entirely using the tag-directed decoding
scheme. Up next, custom decoding of ClassMembers. At that point, all
definition-level decoding will be done, leaving MuIL's ASTs.
2017-01-15 23:57:42 +01:00
|
|
|
}
|
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 18:41:26 +01:00
|
|
|
}
|
Implement custom decoding of ModuleMembers
This change begins to implement some of the AST custom decoding, beneath
the Package's Module map. In particular, we now unmarshal "one level"
beyond this, populating each Module's ModuleMember map. This includes
Classes, Exports, ModuleProperties, and ModuleMethods. The Class AST's
Members have been marked "custom", in addition to Block's Statements,
because they required kind-directed decoding. But Exports and
ModuleProperties can be decoded entirely using the tag-directed decoding
scheme. Up next, custom decoding of ClassMembers. At that point, all
definition-level decoding will be done, leaving MuIL's ASTs.
2017-01-15 23:57:42 +01:00
|
|
|
|
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 18:41:26 +01:00
|
|
|
// 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)
|
2017-01-14 16:40:13 +01:00
|
|
|
}
|