2017-01-14 16:40:13 +01:00
|
|
|
// Copyright 2016 Marapongo, Inc. All rights reserved.
|
|
|
|
|
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
|
|
|
package mapper
|
2017-01-14 16:40:13 +01:00
|
|
|
|
|
|
|
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-01-14 16:40:13 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bag struct {
|
|
|
|
Bool bool
|
|
|
|
BoolP *bool
|
|
|
|
String string
|
|
|
|
StringP *string
|
|
|
|
Float64 float64
|
|
|
|
Float64P *float64
|
2017-01-14 18:42:05 +01:00
|
|
|
Strings []string
|
|
|
|
StringsP *[]string
|
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
|
|
|
func TestFieldMapper(t *testing.T) {
|
|
|
|
md := New(nil)
|
|
|
|
tree := Object{
|
2017-01-14 18:42:05 +01:00
|
|
|
"b": true,
|
|
|
|
"s": "hello",
|
|
|
|
"f": float64(3.14159265359),
|
|
|
|
"ss": []string{"a", "b", "c"},
|
|
|
|
}
|
2017-01-14 16:40:13 +01:00
|
|
|
|
|
|
|
// Try some simple primitive decodes.
|
|
|
|
var s bag
|
|
|
|
var err error
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "b", &s.Bool, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["b"], s.Bool)
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "b", &s.BoolP, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["b"], *s.BoolP)
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "s", &s.String, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["s"], s.String)
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "s", &s.StringP, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["s"], *s.StringP)
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "f", &s.Float64, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["f"], s.Float64)
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "f", &s.Float64P, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["f"], *s.Float64P)
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "ss", &s.Strings, false)
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["ss"], s.Strings)
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "ss", &s.StringsP, false)
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, tree["ss"], *s.StringsP)
|
|
|
|
|
|
|
|
// Ensure interface{} conversions work:
|
|
|
|
var sif string
|
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
|
|
|
err = md.DecodeField(Object{"x": interface{}("hello")}, reflect.TypeOf(bag{}), "x", &sif, false)
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "hello", sif)
|
|
|
|
|
|
|
|
var sifs []string
|
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
|
|
|
err = md.DecodeField(Object{"arr": []interface{}{"a", "b", "c"}}, reflect.TypeOf(bag{}), "arr", &sifs, false)
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, []string{"a", "b", "c"}, sifs)
|
2017-01-14 16:40:13 +01:00
|
|
|
|
|
|
|
// Ensure missing optional fields are ignored:
|
|
|
|
s.String = "x"
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "missing", &s.String, true)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "x", s.String)
|
|
|
|
|
|
|
|
// Try some error conditions; first, wrong type:
|
|
|
|
s.String = "x"
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "b", &s.String, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.NotNil(t, 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
|
|
|
assert.Equal(t, "mapper.bag `b` must be a `string`, got `bool`", err.Error())
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Equal(t, "x", s.String)
|
|
|
|
|
|
|
|
// Next, missing required field:
|
|
|
|
s.String = "x"
|
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
|
|
|
err = md.DecodeField(tree, reflect.TypeOf(bag{}), "missing", &s.String, false)
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.NotNil(t, 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
|
|
|
assert.Equal(t, "Missing required mapper.bag field `missing`", err.Error())
|
2017-01-14 16:40:13 +01:00
|
|
|
assert.Equal(t, "x", s.String)
|
|
|
|
}
|
2017-01-14 18:42:05 +01:00
|
|
|
|
|
|
|
type bagtag struct {
|
|
|
|
String string `json:"s"`
|
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
|
|
|
StringSkip string `json:"sc,skip"`
|
2017-01-14 18:42:05 +01:00
|
|
|
StringOpt string `json:"so,omitempty"`
|
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
|
|
|
StringSkipOpt string `json:"sco,skip,omitempty"`
|
2017-01-14 18:42:05 +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
|
|
|
func TestMapper(t *testing.T) {
|
2017-01-14 18:42:05 +01:00
|
|
|
var err error
|
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
|
|
|
md := New(nil)
|
2017-01-14 18:42:05 +01:00
|
|
|
|
|
|
|
// First, test the fully populated case.
|
|
|
|
var b1 bagtag
|
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
|
|
|
err = md.Decode(Object{
|
2017-01-14 18:42:05 +01:00
|
|
|
"s": "something",
|
|
|
|
"sc": "nothing",
|
|
|
|
"so": "ohmy",
|
|
|
|
"sco": "ohmynada",
|
|
|
|
}, &b1)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "something", b1.String)
|
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
|
|
|
assert.Equal(t, "", b1.StringSkip)
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Equal(t, "ohmy", b1.StringOpt)
|
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
|
|
|
assert.Equal(t, "", b1.StringSkipOpt)
|
2017-01-14 18:42:05 +01:00
|
|
|
|
|
|
|
// Now let optional fields go missing.
|
|
|
|
var b2 bagtag
|
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
|
|
|
err = md.Decode(Object{
|
2017-01-14 18:42:05 +01:00
|
|
|
"s": "something",
|
|
|
|
"sc": "nothing",
|
|
|
|
}, &b2)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "something", b2.String)
|
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
|
|
|
assert.Equal(t, "", b2.StringSkip)
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Equal(t, "", b2.StringOpt)
|
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
|
|
|
assert.Equal(t, "", b2.StringSkipOpt)
|
2017-01-14 18:42:05 +01:00
|
|
|
|
|
|
|
// Try some error conditions; first, wrong type:
|
|
|
|
var b3 bagtag
|
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
|
|
|
err = md.Decode(Object{
|
2017-01-14 18:42:05 +01:00
|
|
|
"s": true,
|
|
|
|
"sc": "",
|
|
|
|
}, &b3)
|
|
|
|
assert.NotNil(t, 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
|
|
|
assert.Equal(t, "mapper.bagtag `s` must be a `string`, got `bool`", err.Error())
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Equal(t, "", b3.String)
|
|
|
|
|
|
|
|
// Next, missing required field:
|
|
|
|
var b4 bagtag
|
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
|
|
|
err = md.Decode(Object{}, &b4)
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.NotNil(t, 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
|
|
|
assert.Equal(t, "Missing required mapper.bagtag field `s`", err.Error())
|
2017-01-14 18:42:05 +01:00
|
|
|
assert.Equal(t, "", b4.String)
|
|
|
|
}
|
2017-01-14 19:06:55 +01:00
|
|
|
|
|
|
|
type bog struct {
|
2017-01-15 22:50:00 +01:00
|
|
|
Boggy bogger `json:"boggy"`
|
|
|
|
BoggyP *bogger `json:"boggyp"`
|
|
|
|
Boggers []bogger `json:"boggers"`
|
|
|
|
BoggersP *[]*bogger `json:"boggersp"`
|
2017-01-14 19:06:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type bogger struct {
|
|
|
|
Num float64 `json:"num"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func TestNestedMapper(t *testing.T) {
|
|
|
|
md := New(nil)
|
|
|
|
|
2017-01-15 22:50:00 +01:00
|
|
|
// Test one level deep nesting (fields, arrays, pointers).
|
2017-01-14 19:06:55 +01:00
|
|
|
var b bog
|
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
|
|
|
err := md.Decode(Object{
|
|
|
|
"boggy": Object{"num": float64(99)},
|
|
|
|
"boggyp": Object{"num": float64(180)},
|
|
|
|
"boggers": []Object{
|
2017-01-14 19:06:55 +01:00
|
|
|
{"num": float64(1)},
|
|
|
|
{"num": float64(2)},
|
|
|
|
{"num": float64(42)},
|
|
|
|
},
|
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
|
|
|
"boggersp": []Object{
|
2017-01-15 22:50:00 +01:00
|
|
|
{"num": float64(4)},
|
|
|
|
{"num": float64(8)},
|
|
|
|
{"num": float64(84)},
|
|
|
|
},
|
2017-01-14 19:06:55 +01:00
|
|
|
}, &b)
|
|
|
|
assert.Nil(t, err)
|
2017-01-15 22:50:00 +01:00
|
|
|
assert.Equal(t, float64(99), b.Boggy.Num)
|
|
|
|
assert.NotNil(t, b.BoggyP)
|
|
|
|
assert.Equal(t, float64(180), b.BoggyP.Num)
|
2017-01-14 19:06:55 +01:00
|
|
|
assert.Equal(t, 3, len(b.Boggers))
|
|
|
|
assert.Equal(t, float64(1), b.Boggers[0].Num)
|
|
|
|
assert.Equal(t, float64(2), b.Boggers[1].Num)
|
|
|
|
assert.Equal(t, float64(42), b.Boggers[2].Num)
|
2017-01-15 22:50:00 +01:00
|
|
|
assert.NotNil(t, b.BoggersP)
|
|
|
|
assert.Equal(t, 3, len(*b.BoggersP))
|
|
|
|
assert.NotNil(t, (*b.BoggersP)[0])
|
|
|
|
assert.Equal(t, float64(4), (*b.BoggersP)[0].Num)
|
|
|
|
assert.NotNil(t, (*b.BoggersP)[1])
|
|
|
|
assert.Equal(t, float64(8), (*b.BoggersP)[1].Num)
|
|
|
|
assert.NotNil(t, (*b.BoggersP)[2])
|
|
|
|
assert.Equal(t, float64(84), (*b.BoggersP)[2].Num)
|
|
|
|
}
|
|
|
|
|
|
|
|
type boggerdybogger struct {
|
|
|
|
Bogs map[string]bog `json:"bogs"`
|
|
|
|
BogsP *map[string]*bog `json:"bogsp"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func TestMultiplyNestedMapper(t *testing.T) {
|
|
|
|
md := New(nil)
|
|
|
|
|
2017-01-15 22:50:00 +01:00
|
|
|
// Test multilevel nesting (maps, fields, arrays, pointers).
|
|
|
|
var ber boggerdybogger
|
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
|
|
|
err := md.Decode(Object{
|
|
|
|
"bogs": Object{
|
|
|
|
"a": Object{
|
|
|
|
"boggy": Object{"num": float64(99)},
|
|
|
|
"boggyp": Object{"num": float64(180)},
|
|
|
|
"boggers": []Object{
|
2017-01-15 22:50:00 +01:00
|
|
|
{"num": float64(1)},
|
|
|
|
{"num": float64(2)},
|
|
|
|
{"num": float64(42)},
|
|
|
|
},
|
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
|
|
|
"boggersp": []Object{
|
2017-01-15 22:50:00 +01:00
|
|
|
{"num": float64(4)},
|
|
|
|
{"num": float64(8)},
|
|
|
|
{"num": float64(84)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
"bogsp": Object{
|
|
|
|
"z": Object{
|
|
|
|
"boggy": Object{"num": float64(188)},
|
|
|
|
"boggyp": Object{"num": float64(360)},
|
|
|
|
"boggers": []Object{
|
2017-01-15 22:50:00 +01:00
|
|
|
{"num": float64(2)},
|
|
|
|
{"num": float64(4)},
|
|
|
|
{"num": float64(84)},
|
|
|
|
},
|
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
|
|
|
"boggersp": []Object{
|
2017-01-15 22:50:00 +01:00
|
|
|
{"num": float64(8)},
|
|
|
|
{"num": float64(16)},
|
|
|
|
{"num": float64(168)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, &ber)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(ber.Bogs))
|
|
|
|
b := ber.Bogs["a"]
|
|
|
|
assert.Equal(t, float64(99), b.Boggy.Num)
|
|
|
|
assert.NotNil(t, b.BoggyP)
|
|
|
|
assert.Equal(t, float64(180), b.BoggyP.Num)
|
|
|
|
assert.Equal(t, 3, len(b.Boggers))
|
|
|
|
assert.Equal(t, float64(1), b.Boggers[0].Num)
|
|
|
|
assert.Equal(t, float64(2), b.Boggers[1].Num)
|
|
|
|
assert.Equal(t, float64(42), b.Boggers[2].Num)
|
|
|
|
assert.NotNil(t, b.BoggersP)
|
|
|
|
assert.Equal(t, 3, len(*b.BoggersP))
|
|
|
|
assert.NotNil(t, (*b.BoggersP)[0])
|
|
|
|
assert.Equal(t, float64(4), (*b.BoggersP)[0].Num)
|
|
|
|
assert.NotNil(t, (*b.BoggersP)[1])
|
|
|
|
assert.Equal(t, float64(8), (*b.BoggersP)[1].Num)
|
|
|
|
assert.NotNil(t, (*b.BoggersP)[2])
|
|
|
|
assert.Equal(t, float64(84), (*b.BoggersP)[2].Num)
|
|
|
|
|
|
|
|
assert.NotNil(t, ber.BogsP)
|
|
|
|
assert.Equal(t, 1, len(*ber.BogsP))
|
|
|
|
p := (*ber.BogsP)["z"]
|
|
|
|
assert.NotNil(t, p)
|
|
|
|
assert.Equal(t, float64(188), p.Boggy.Num)
|
|
|
|
assert.NotNil(t, p.BoggyP)
|
|
|
|
assert.Equal(t, float64(360), p.BoggyP.Num)
|
|
|
|
assert.Equal(t, 3, len(p.Boggers))
|
|
|
|
assert.Equal(t, float64(2), p.Boggers[0].Num)
|
|
|
|
assert.Equal(t, float64(4), p.Boggers[1].Num)
|
|
|
|
assert.Equal(t, float64(84), p.Boggers[2].Num)
|
|
|
|
assert.NotNil(t, p.BoggersP)
|
|
|
|
assert.Equal(t, 3, len(*p.BoggersP))
|
|
|
|
assert.NotNil(t, (*p.BoggersP)[0])
|
|
|
|
assert.Equal(t, float64(8), (*p.BoggersP)[0].Num)
|
|
|
|
assert.NotNil(t, (*p.BoggersP)[1])
|
|
|
|
assert.Equal(t, float64(16), (*p.BoggersP)[1].Num)
|
|
|
|
assert.NotNil(t, (*p.BoggersP)[2])
|
|
|
|
assert.Equal(t, float64(168), (*p.BoggersP)[2].Num)
|
2017-01-14 19:06:55 +01:00
|
|
|
}
|
2017-01-14 20:21:27 +01:00
|
|
|
|
|
|
|
type hasmap struct {
|
|
|
|
Entries map[string]mapentry `json:"entries"`
|
|
|
|
EntriesP map[string]*mapentry `json:"entriesp"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapentry struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func TestMapMapper(t *testing.T) {
|
|
|
|
md := New(nil)
|
|
|
|
|
2017-01-14 20:21:27 +01:00
|
|
|
// Ensure we can decode both maps of structs and maps of pointers to structs.
|
|
|
|
var hm hasmap
|
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
|
|
|
err := md.Decode(Object{
|
|
|
|
"entries": Object{
|
|
|
|
"a": Object{"title": "first"},
|
|
|
|
"b": Object{"title": "second"},
|
2017-01-14 20:21:27 +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
|
|
|
"entriesp": Object{
|
|
|
|
"x": Object{"title": "firstp"},
|
|
|
|
"y": Object{"title": "secondp"},
|
2017-01-14 20:21:27 +01:00
|
|
|
},
|
|
|
|
}, &hm)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 2, len(hm.Entries))
|
|
|
|
assert.Equal(t, "first", hm.Entries["a"].Title)
|
|
|
|
assert.Equal(t, "second", hm.Entries["b"].Title)
|
|
|
|
assert.Equal(t, 2, len(hm.EntriesP))
|
|
|
|
assert.NotNil(t, hm.EntriesP["x"])
|
|
|
|
assert.NotNil(t, hm.EntriesP["y"])
|
|
|
|
assert.Equal(t, "firstp", hm.EntriesP["x"].Title)
|
|
|
|
assert.Equal(t, "secondp", hm.EntriesP["y"].Title)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
type wrap struct {
|
|
|
|
C customStruct `json:"c"`
|
|
|
|
CI customInterface `json:"ci"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type customInterface interface {
|
|
|
|
GetX() float64
|
|
|
|
GetY() float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type customStruct struct {
|
|
|
|
X float64 `json:"x"`
|
|
|
|
Y float64 `json:"y"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *customStruct) GetX() float64 { return s.X }
|
|
|
|
func (s *customStruct) GetY() float64 { return s.Y }
|
|
|
|
|
|
|
|
func TestCustomMapper(t *testing.T) {
|
|
|
|
var md Mapper
|
|
|
|
md = New(Decoders{
|
|
|
|
reflect.TypeOf((*customInterface)(nil)).Elem(): decodeCustomInterface,
|
|
|
|
reflect.TypeOf(customStruct{}): decodeCustomStruct,
|
|
|
|
})
|
|
|
|
|
|
|
|
var w wrap
|
|
|
|
err := md.Decode(Object{
|
|
|
|
"c": Object{
|
|
|
|
"x": float64(-99.2),
|
|
|
|
"y": float64(127.127),
|
|
|
|
},
|
|
|
|
"ci": Object{
|
|
|
|
"x": float64(42.6),
|
|
|
|
"y": float64(247.9),
|
|
|
|
},
|
|
|
|
}, &w)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, float64(-99.2), w.C.X)
|
|
|
|
assert.Equal(t, float64(127.127), w.C.Y)
|
|
|
|
assert.NotNil(t, w.CI)
|
|
|
|
assert.Equal(t, float64(42.6), w.CI.GetX())
|
|
|
|
assert.Equal(t, float64(247.9), w.CI.GetY())
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeCustomInterface(m Mapper, tree Object) (interface{}, error) {
|
|
|
|
var s customStruct
|
|
|
|
if err := m.DecodeField(tree, reflect.TypeOf(s), "x", &s.X, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := m.DecodeField(tree, reflect.TypeOf(s), "y", &s.Y, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return customInterface(&s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeCustomStruct(m Mapper, tree Object) (interface{}, error) {
|
|
|
|
var s customStruct
|
|
|
|
if err := m.DecodeField(tree, reflect.TypeOf(s), "x", &s.X, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := m.DecodeField(tree, reflect.TypeOf(s), "y", &s.Y, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|