open-nomad/nomad/structs/encoding.go

39 lines
1.2 KiB
Go
Raw Normal View History

package structs
import (
"reflect"
"github.com/hashicorp/go-msgpack/codec"
)
// extendFunc is a mapping from one struct to another, to change the shape of the encoded JSON
2021-03-22 01:49:21 +00:00
type extendFunc func(interface{}) interface{}
// nomadJsonEncodingExtensions is a catch-all go-msgpack extension
// it looks up the types in the list of registered extension functions and applies it
2021-03-22 01:49:21 +00:00
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 required by go-msgpack, but not used by us
2021-03-22 01:49:21 +00:00
func (n nomadJsonEncodingExtensions) UpdateExt(_ interface{}, _ interface{}) {}
// NomadJsonEncodingExtensions registers all extension functions against the
// provided JsonHandle.
// It should be called on any JsonHandle which is used by the API HTTP server.
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
}