Switch to using go/codec and use code generation

This commit is contained in:
Alex Dadgar 2016-02-20 18:05:17 -08:00
parent 797a8a4138
commit 2a19e179bc
9 changed files with 30 additions and 10 deletions

2
.gitignore vendored
View File

@ -48,6 +48,8 @@ example.nomad
nomad_linux_amd64
nomad_darwin_amd64
TODO.md
*.generated.go
.terraform
*.tfstate*

View File

@ -13,10 +13,10 @@ GOFILES_NOVENDOR = $(shell find . -type f -name '*.go' -not -path "./vendor/*")
all: test
dev: format
dev: format generate
@NOMAD_DEV=1 sh -c "'$(PWD)/scripts/build.sh'"
bin:
bin: generate
@sh -c "'$(PWD)/scripts/build.sh'"
release:
@ -26,7 +26,7 @@ cov:
gocov test ./... | gocov-html > /tmp/coverage.html
open /tmp/coverage.html
test:
test: generate
@sh -c "'$(PWD)/scripts/test.sh'"
@$(MAKE) vet
@ -37,6 +37,10 @@ format:
@echo "--> Running go fmt"
@go fmt $(PACKAGES)
generate:
@echo "--> Running go generate"
@go generate $(PACKAGES)
vet:
@go tool vet 2>/dev/null ; if [ $$? -eq 3 ]; then \
go get golang.org/x/tools/cmd/vet; \

View File

@ -407,4 +407,3 @@ func TestCoreScheduler_JobGC_Force(t *testing.T) {
}
}
}

View File

@ -7,10 +7,10 @@ import (
"time"
"github.com/armon/go-metrics"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/nomad/nomad/state"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/raft"
"github.com/ugorji/go/codec"
)
const (

View File

@ -55,13 +55,13 @@ const (
// NewClientCodec returns a new rpc.ClientCodec to be used to make RPC calls to
// the Nomad Server.
func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec {
return msgpackrpc.NewCodecFromHandle(true, true, conn, structs.MsgpackHandle)
return msgpackrpc.NewCodecFromHandle(true, true, conn, structs.HashiMsgpackHandle)
}
// NewServerCodec returns a new rpc.ServerCodec to be used by the Nomad Server
// to handle rpcs.
func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec {
return msgpackrpc.NewCodecFromHandle(true, true, conn, structs.MsgpackHandle)
return msgpackrpc.NewCodecFromHandle(true, true, conn, structs.HashiMsgpackHandle)
}
// listen is used to listen for incoming RPC connections

View File

@ -13,11 +13,13 @@ import (
"time"
"github.com/gorhill/cronexpr"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-version"
"github.com/hashicorp/nomad/helper/args"
"github.com/mitchellh/copystructure"
"github.com/ugorji/go/codec"
hcodec "github.com/hashicorp/go-msgpack/codec"
)
var (
@ -2501,6 +2503,16 @@ var MsgpackHandle = func() *codec.MsgpackHandle {
return h
}()
var HashiMsgpackHandle = func() *hcodec.MsgpackHandle {
h := &hcodec.MsgpackHandle{RawToString: true}
// Sets the default type for decoding a map into a nil interface{}.
// This is necessary in particular because we store the driver configs as a
// nil interface{}.
h.MapType = reflect.TypeOf(map[string]interface{}(nil))
return h
}()
// Decode is used to decode a MsgPack encoded object
func Decode(buf []byte, out interface{}) error {
return codec.NewDecoder(bytes.NewReader(buf), MsgpackHandle).Decode(out)

View File

@ -0,0 +1,3 @@
package structs
//go:generate codecgen -o structs.generated.go structs.go

View File

@ -5,7 +5,7 @@ import (
"sync"
"time"
"github.com/hashicorp/go-msgpack/codec"
"github.com/ugorji/go/codec"
)
// TimeTable is used to associate a Raft index with a timestamp.

View File

@ -6,8 +6,8 @@ import (
"testing"
"time"
"github.com/hashicorp/go-msgpack/codec"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/ugorji/go/codec"
)
func TestTimeTable(t *testing.T) {