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.
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
package encoding
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/marapongo/mu/pkg/pack/ast"
|
|
"github.com/marapongo/mu/pkg/util/contract"
|
|
"github.com/marapongo/mu/pkg/util/mapper"
|
|
)
|
|
|
|
func decodeModuleMember(m mapper.Mapper, tree mapper.Object) (ast.ModuleMember, error) {
|
|
k, err := mapper.FieldString(tree, reflect.TypeOf((*ast.ModuleMember)(nil)).Elem(), "kind", true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if k != nil {
|
|
kind := ast.NodeKind(*k)
|
|
switch kind {
|
|
case ast.ClassKind:
|
|
return decodeClass(m, tree)
|
|
case ast.ExportKind:
|
|
return decodeExport(m, tree)
|
|
case ast.ModulePropertyKind:
|
|
return decodeModuleProperty(m, tree)
|
|
case ast.ModuleMethodKind:
|
|
return decodeModuleMethod(m, tree)
|
|
default:
|
|
contract.FailMF("Unrecognized ModuleMember kind: %v\n", kind)
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func decodeClass(m mapper.Mapper, tree mapper.Object) (*ast.Class, error) {
|
|
var class ast.Class
|
|
if err := m.Decode(tree, &class); err != nil {
|
|
return nil, err
|
|
}
|
|
return &class, nil
|
|
}
|
|
|
|
func decodeClassMember(m mapper.Mapper, tree mapper.Object) (ast.ClassMember, error) {
|
|
k, err := mapper.FieldString(tree, reflect.TypeOf((*ast.ClassMember)(nil)).Elem(), "kind", true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if k != nil {
|
|
kind := ast.NodeKind(*k)
|
|
switch kind {
|
|
case ast.ClassPropertyKind:
|
|
return decodeClassProperty(m, tree)
|
|
case ast.ClassMethodKind:
|
|
return decodeClassMethod(m, tree)
|
|
default:
|
|
contract.FailMF("Unrecognized ClassMember kind: %v\n", kind)
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func decodeClassProperty(m mapper.Mapper, tree mapper.Object) (*ast.ClassProperty, error) {
|
|
var prop ast.ClassProperty
|
|
if err := m.Decode(tree, &prop); err != nil {
|
|
return nil, err
|
|
}
|
|
return &prop, nil
|
|
}
|
|
|
|
func decodeClassMethod(m mapper.Mapper, tree mapper.Object) (*ast.ClassMethod, error) {
|
|
var meth ast.ClassMethod
|
|
if err := m.Decode(tree, &meth); err != nil {
|
|
return nil, err
|
|
}
|
|
return &meth, nil
|
|
}
|
|
|
|
func decodeExport(m mapper.Mapper, tree mapper.Object) (*ast.Export, error) {
|
|
var export ast.Export
|
|
if err := m.Decode(tree, &export); err != nil {
|
|
return nil, err
|
|
}
|
|
return &export, nil
|
|
}
|
|
|
|
func decodeModuleProperty(m mapper.Mapper, tree mapper.Object) (*ast.ModuleProperty, error) {
|
|
var prop ast.ModuleProperty
|
|
if err := m.Decode(tree, &prop); err != nil {
|
|
return nil, err
|
|
}
|
|
return &prop, nil
|
|
}
|
|
|
|
func decodeModuleMethod(m mapper.Mapper, tree mapper.Object) (*ast.ModuleMethod, error) {
|
|
var meth ast.ModuleMethod
|
|
if err := m.Decode(tree, &meth); err != nil {
|
|
return nil, err
|
|
}
|
|
return &meth, nil
|
|
}
|