From 88808b2d3003bd7937c769f36ddc6bc2b4e31f56 Mon Sep 17 00:00:00 2001 From: Mahmood Ali Date: Mon, 11 May 2020 14:14:10 -0400 Subject: [PATCH] When serializing msgpack, only consider codec tag When serializing structs with msgpack, only consider type tags of `codec`. Hashicorp/go-msgpack (based on ugorji/go) defaults to interpretting `codec` tag if it's available, but falls to using `json` if `codec` isn't present. This behavior is surprising in cases where we want to serialize json differently from msgpack, e.g. serializing `ConsulExposeConfig`. --- client/structs/structs.go | 2 +- nomad/structs/generate.sh | 1 + nomad/structs/structs.go | 3 +++ plugins/base/plugin.go | 4 ++++ 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/client/structs/structs.go b/client/structs/structs.go index 273205652..c8976802c 100644 --- a/client/structs/structs.go +++ b/client/structs/structs.go @@ -1,6 +1,6 @@ package structs -//go:generate codecgen -c github.com/hashicorp/go-msgpack/codec -d 102 -t codegen_generated -o structs.generated.go structs.go +//go:generate codecgen -c github.com/hashicorp/go-msgpack/codec -st codec -d 102 -t codegen_generated -o structs.generated.go structs.go import ( "errors" diff --git a/nomad/structs/generate.sh b/nomad/structs/generate.sh index 0d799fed3..93107092b 100755 --- a/nomad/structs/generate.sh +++ b/nomad/structs/generate.sh @@ -4,6 +4,7 @@ set -e FILES="$(ls ./*.go | grep -v -e _test.go -e .generated.go | tr '\n' ' ')" codecgen \ -c github.com/hashicorp/go-msgpack/codec \ + -st codec \ -d 100 \ -t codegen_generated \ -o structs.generated.go \ diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 7d4ebba65..9b6c72dad 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -9561,6 +9561,9 @@ var MsgpackHandle = func() *codec.MsgpackHandle { // nil interface{}. h.MapType = reflect.TypeOf(map[string]interface{}(nil)) + // only review struct codec tags + h.TypeInfos = codec.NewTypeInfos([]string{"codec"}) + return h }() diff --git a/plugins/base/plugin.go b/plugins/base/plugin.go index 74bcaa0c7..735dfd43f 100644 --- a/plugins/base/plugin.go +++ b/plugins/base/plugin.go @@ -65,6 +65,10 @@ var MsgpackHandle = func() *codec.MsgpackHandle { h.BasicHandle.TimeNotBuiltin = true h.MapType = reflect.TypeOf(map[string]interface{}(nil)) + + // only review struct codec tags - ignore `json` flags + h.TypeInfos = codec.NewTypeInfos([]string{"codec"}) + return h }()