Skip to content

Commit

Permalink
Switch to using go/codec and use code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Feb 21, 2016
1 parent ea849f6 commit 6b188d8
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ example.nomad
nomad_linux_amd64
nomad_darwin_amd64
TODO.md
*.generated.go


.terraform
*.tfstate*
10 changes: 7 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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; \
Expand Down
1 change: 0 additions & 1 deletion nomad/core_sched_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,3 @@ func TestCoreScheduler_JobGC_Force(t *testing.T) {
}
}
}

2 changes: 1 addition & 1 deletion nomad/fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions nomad/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions nomad/structs/structs_codegen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package structs

//go:generate codecgen -o structs.generated.go structs.go
2 changes: 1 addition & 1 deletion nomad/timetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion nomad/timetable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6b188d8

Please sign in to comment.