2021-04-02 13:31:10 +00:00
|
|
|
package structs
|
2021-03-26 17:03:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// extendedTypes is a mapping of extended types to their extension function
|
|
|
|
// TODO: the duplicates could be simplified by looking up the base type in the case of a pointer type in ConvertExt
|
|
|
|
extendedTypes = map[reflect.Type]extendFunc{
|
2021-04-02 13:31:10 +00:00
|
|
|
reflect.TypeOf(Node{}): nodeExt,
|
|
|
|
reflect.TypeOf(&Node{}): nodeExt,
|
2021-03-26 17:03:15 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// nodeExt ensures the node is sanitized and adds the legacy field .Drain back to encoded Node objects
|
|
|
|
func nodeExt(v interface{}) interface{} {
|
2021-04-02 13:31:10 +00:00
|
|
|
node := v.(*Node).Sanitize()
|
2021-03-26 17:03:15 +00:00
|
|
|
// transform to a struct with inlined Node fields plus the Drain field
|
|
|
|
// - using defined type (not an alias!) EmbeddedNode gives us free conversion to a distinct type
|
|
|
|
// - distinct type prevents this encoding extension from being called recursively/infinitely on the embedding
|
|
|
|
// - pointers mean the conversion function doesn't have to make a copy during conversion
|
2021-04-02 13:31:10 +00:00
|
|
|
type EmbeddedNode Node
|
2021-03-26 17:03:15 +00:00
|
|
|
return &struct {
|
|
|
|
*EmbeddedNode
|
|
|
|
Drain bool
|
|
|
|
}{
|
|
|
|
EmbeddedNode: (*EmbeddedNode)(node),
|
|
|
|
Drain: node != nil && node.DrainStrategy != nil,
|
|
|
|
}
|
|
|
|
}
|