2021-02-11 15:40:59 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
|
|
|
)
|
|
|
|
|
2021-03-22 01:49:21 +00:00
|
|
|
func init() {
|
|
|
|
registerExtension(reflect.TypeOf(Node{}), nodeExt)
|
|
|
|
}
|
2021-02-11 15:40:59 +00:00
|
|
|
|
2021-03-22 01:49:21 +00:00
|
|
|
func nodeExt(v interface{}) interface{} {
|
2021-02-11 15:40:59 +00:00
|
|
|
node := v.(*Node)
|
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
type NodeAlias Node
|
|
|
|
return &struct {
|
|
|
|
*NodeAlias
|
|
|
|
Drain bool
|
|
|
|
}{
|
|
|
|
NodeAlias: (*NodeAlias)(node),
|
|
|
|
Drain: node.DrainStrategy != nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 01:49:21 +00:00
|
|
|
// BOILERPLATE GOES HERE
|
|
|
|
|
|
|
|
type extendFunc func(interface{}) interface{}
|
|
|
|
|
|
|
|
var (
|
|
|
|
extendedTypes = map[reflect.Type]extendFunc{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func registerExtension(tpe reflect.Type, ext extendFunc) {
|
|
|
|
extendedTypes[tpe] = ext
|
|
|
|
}
|
|
|
|
|
|
|
|
type nomadJsonEncodingExtensions struct{}
|
|
|
|
|
|
|
|
// ConvertExt calls the registered conversions functions
|
|
|
|
func (n nomadJsonEncodingExtensions) ConvertExt(v interface{}) interface{} {
|
|
|
|
if fn, ok := extendedTypes[reflect.TypeOf(v)]; ok {
|
|
|
|
return fn(v)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:40:59 +00:00
|
|
|
// UpdateExt is not used
|
2021-03-22 01:49:21 +00:00
|
|
|
func (n nomadJsonEncodingExtensions) UpdateExt(_ interface{}, _ interface{}) {}
|
2021-02-11 15:40:59 +00:00
|
|
|
|
2021-03-22 01:49:21 +00:00
|
|
|
func NomadJsonEncodingExtensions(h *codec.JsonHandle) *codec.JsonHandle {
|
|
|
|
for tpe, _ := range extendedTypes {
|
|
|
|
h.SetInterfaceExt(tpe, 1, nomadJsonEncodingExtensions{})
|
|
|
|
}
|
2021-02-11 15:40:59 +00:00
|
|
|
return h
|
|
|
|
}
|