open-nomad/nomad/json/encoding.go

41 lines
951 B
Go
Raw Normal View History

package json
import (
"reflect"
"github.com/hashicorp/go-msgpack/codec"
)
2021-03-22 01:49:21 +00:00
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 {
2021-03-23 18:13:10 +00:00
// shouldn't get here, but returning v will probably result in an infinite loop
// return nil and erase this field
return nil
2021-03-22 01:49:21 +00:00
}
}
// UpdateExt is not used
2021-03-22 01:49:21 +00:00
func (n nomadJsonEncodingExtensions) UpdateExt(_ interface{}, _ interface{}) {}
2021-03-22 01:49:21 +00:00
func NomadJsonEncodingExtensions(h *codec.JsonHandle) *codec.JsonHandle {
for tpe := range extendedTypes {
2021-03-22 01:49:21 +00:00
h.SetInterfaceExt(tpe, 1, nomadJsonEncodingExtensions{})
}
return h
}