Skip to content

Commit

Permalink
Merge branch 'master' into stats-task-no-global
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Feb 24, 2017
2 parents d66338c + 732bd07 commit f6f8452
Show file tree
Hide file tree
Showing 60 changed files with 883 additions and 2,241 deletions.
2 changes: 1 addition & 1 deletion .build/flags.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PKGS ?= $(shell glide novendor)
LIST_PKGS ?= $(shell go list ./... | grep -v /vendor/)

# Many Go tools take file globs or directories as arguments instead of packages.
PKG_FILES ?= *.go config internal metrics modules service tracing ulog
PKG_FILES ?= *.go auth config dig internal metrics modules service testutils tracing ulog

# The linting tools evolve with each Go version, so run them only on the latest
# stable release.
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ language: go
sudo: false
go:
- 1.7
- 1.8
- tip

go_import_path: go.uber.org/fx

env:
- V=1
- V=1 COVERMODE=atomic

matrix:
# Fail the build as soon as the first job fails
Expand All @@ -23,6 +24,7 @@ install:
script:
- make lint
- make test
- make examples

after_success:
- '[ "${TRAVIS_GO_VERSION}" != "tip" ] && make coveralls'
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.0.0-beta2 (unreleased)

* [Breaking] Remove `ulog` and expose `*zap.Logger` directly.
* [Breaking] Upgrade `zap` to `v1.0.0-rc.2` (now go.uber.org/zap, was
github.com/uber-go/zap)

## v1.0.0-beta1 (20 Feb 2017)

This is the first beta release of the framework, where we invite users to start
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:1.8.0

WORKDIR /go/src/go.uber.org/fx
ADD .build/deps.mk /go/src/go.uber.org/fx/.build/
ADD glide.yaml glide.lock /go/src/go.uber.org/fx/
RUN make -f .build/deps.mk deps
ADD . /go/src/go.uber.org/fx/
RUN make clean
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ all: lint test

COV_REPORT := overalls.coverprofile

DOCKER_IMAGE := uber/fx

# all .go files that don't exist in hidden directories
ALL_SRC := $(shell find . -name "*.go" | grep -v -e vendor \
-e ".*/\..*" \
-e "examples/keyvalue/.*" \
-e "examples/.*" \
-e ".*/_.*")

TEST_TIMEOUT := "-timeout=10s"

.PHONY: test
test: examples $(COV_REPORT)
test: $(COV_REPORT)

TEST_IGNORES = vendor .git
COVER_IGNORES = $(TEST_IGNORES) examples testutils

COVERMODE ?= set

comma := ,
null :=
space := $(null) #
Expand Down Expand Up @@ -57,7 +61,7 @@ $(COV_REPORT): $(PKG_FILES) $(ALL_SRC)
-project=$(PROJECT_ROOT) \
-go-binary=richgo \
-ignore "$(OVERALLS_IGNORE)" \
-covermode=atomic \
-covermode=$(COVERMODE) \
$(DEBUG_FLAG) -- \
$(TEST_FLAGS) $(RACE) $(TEST_TIMEOUT) $(TEST_VERBOSITY_FLAG) | \
grep -v "No Go Test files" | \
Expand Down Expand Up @@ -121,6 +125,18 @@ benchreset:
$(ECHO_V)rm -f $(BENCH_FILE)


.PHONY: dockerbuild
dockerbuild:
docker build -t $(DOCKER_IMAGE) .

.PHONY: dockerlint
dockerlint: dockerbuild
docker run $(DOCKER_IMAGE) make lint

.PHONY: dockertest
dockertest: dockerbuild
docker run $(DOCKER_IMAGE) make test

include $(SUPPORT_FILES)/lint.mk
include $(SUPPORT_FILES)/licence.mk

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ modules:
timeout: 60s
```

In this example, a module named: "rpc" would look up its advertise name as
`modules.rpc.advertiseName`.
In this example, a module named: "yarpc" would look up its advertise name as
`modules.yarpc.advertiseName`.

## Metrics

Expand Down
6 changes: 3 additions & 3 deletions auth/uauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
"testing"

"go.uber.org/fx/config"
"go.uber.org/fx/ulog"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uber-go/tally"
"go.uber.org/zap"
)

func withAuthClientSetup(t *testing.T, registerFunc RegisterFunc, info CreateAuthInfo, fn func()) {
Expand Down Expand Up @@ -86,8 +86,8 @@ func (fakeAuthInfo) Config() config.Provider {
return nil
}

func (fakeAuthInfo) Logger() ulog.Log {
return ulog.NopLogger
func (fakeAuthInfo) Logger() *zap.Logger {
return zap.NewNop()
}

func (fakeAuthInfo) Metrics() tally.Scope {
Expand Down
29 changes: 29 additions & 0 deletions config/static_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,32 @@ func TestStaticProviderFmtPrintOnValueNoPanic(t *testing.T) {
}
assert.NotPanics(t, f)
}

func TestNilStaticProviderSetDefaultTagValue(t *testing.T) {
type Inner struct {
Set bool `yaml:"set" default:"true"`
}
data := struct {
ID0 int `yaml:"id0" default:"10"`
ID1 string `yaml:"id1" default:"string"`
ID2 Inner `yaml:"id2"`
ID3 []Inner `yaml:"id3"`
ID4 map[Inner]Inner `yaml:"id4"`
ID5 *Inner `yaml:"id5"`
//ID6 [6]Inner `yaml:"id6"`
ID7 [7]*Inner `yaml:"id7"`
}{}

p := NewStaticProvider(nil)
p.Get("hello").PopulateStruct(&data)

assert.Equal(t, 10, data.ID0)
assert.Equal(t, "string", data.ID1)
assert.True(t, data.ID2.Set)
assert.Nil(t, data.ID3)
assert.Nil(t, data.ID4)
assert.Nil(t, data.ID5)
// TODO (yutong) uncomment following assert after DRI-12.
// assert.True(t, data.ID6[0].Set)
assert.Nil(t, data.ID7[0])
}
31 changes: 16 additions & 15 deletions config/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,7 @@ func convertValue(value interface{}, targetType reflect.Type) (interface{}, erro

// PopulateStruct fills in a struct from configuration
func (cv Value) PopulateStruct(target interface{}) error {
if !cv.HasValue() {
return nil
}

_, err := cv.valueStruct(cv.key, target)

return err
}

Expand Down Expand Up @@ -519,16 +514,20 @@ func (cv Value) valueStruct(key string, target interface{}) (interface{}, error)
case bucketObject:
ntt := derefType(fieldType)
newTarget := reflect.New(ntt)
if v2 := global.Get(childKey); v2.HasValue() {

if err := v2.PopulateStruct(newTarget.Interface()); err != nil {
return nil, errors.Wrap(err, "unable to populate struct of object target")
}
// if the target is not a pointer, deref the value
// for copy semantics
if fieldType.Kind() != reflect.Ptr {
newTarget = newTarget.Elem()
}
v2 := global.Get(childKey)
if !v2.HasValue() && fieldType.Kind() == reflect.Ptr {
// in this case we will keep the pointer value as not defined.
continue
}
if err := v2.PopulateStruct(newTarget.Interface()); err != nil {
return nil, errors.Wrap(err, "unable to populate struct of object target")
}
// if the target is not a pointer, deref the value
// for copy semantics
if fieldType.Kind() != reflect.Ptr {
newTarget = newTarget.Elem()
}
if fieldValue.CanSet() {
fieldValue.Set(newTarget)
}
case bucketArray:
Expand All @@ -552,6 +551,8 @@ func (cv Value) valueStruct(key string, target interface{}) (interface{}, error)
}
case bucketObject:
newTarget := reflect.New(elementType)
// for slice, there is no need to set default value on inner struct
// when input is empty, so only continue when v2 has value.
if v2 := global.Get(arrayKey); v2.HasValue() {
if err := v2.PopulateStruct(newTarget.Interface()); err != nil {
return nil, errors.Wrap(err, "unable to populate struct of object")
Expand Down
55 changes: 55 additions & 0 deletions config/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,58 @@ func TestArrayTypeNoPanic(t *testing.T) {

assert.NoError(t, provider.Get(Root).PopulateStruct(&cs))
}

func TestNilYAMLProviderSetDefaultTagValue(t *testing.T) {
type Inner struct {
Set bool `yaml:"set" default:"true"`
}
data := struct {
ID0 int `yaml:"id0" default:"10"`
ID1 string `yaml:"id1" default:"string"`
ID2 Inner `yaml:"id2"`
ID3 []Inner `yaml:"id3"`
ID4 map[Inner]Inner `yaml:"id4"`
ID5 *Inner `yaml:"id5"`
//ID6 [6]Inner `yaml:"id6"`
ID7 [7]*Inner `yaml:"id7"`
}{}

p := NewYAMLProviderFromBytes(nil)
p.Get("hello").PopulateStruct(&data)

assert.Equal(t, 10, data.ID0)
assert.Equal(t, "string", data.ID1)
assert.True(t, data.ID2.Set)
assert.Nil(t, data.ID3)
assert.Nil(t, data.ID4)
assert.Nil(t, data.ID5)
// TODO (yutong) uncomment following assert after DRI-12.
// assert.True(t, data.ID6[0].Set)
assert.Nil(t, data.ID7[0])
}

func TestDefaultWithMergeConfig(t *testing.T) {
base := []byte(`
abc:
str: "base"
int: 1
`)

prod := []byte(`
abc:
str: "prod"
`)
cfg := struct {
Str string `yaml:"str" default:"nope"`
Int int `yaml:"int" default:"0"`
Bool bool `yaml:"bool" default:"true"`
BoolPtr *bool `yaml:"bool_ptr"`
}{}
p := NewYAMLProviderFromBytes(base, prod)
p.Get("abc").PopulateStruct(&cfg)

assert.Equal(t, "prod", cfg.Str)
assert.Equal(t, 1, cfg.Int)
assert.Equal(t, true, cfg.Bool)
assert.Nil(t, cfg.BoolPtr)
}
4 changes: 2 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
// port: 8080
// timeout: 60s
//
// In this example, a module named: "rpc" would look up its advertise name as
// modules.rpc.advertiseName.
// In this example, a module named: "yarpc" would look up its advertise name as
// modules.yarpc.advertiseName.
//
// Metrics
//
Expand Down
11 changes: 5 additions & 6 deletions examples/keyvalue/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ import (
"go.uber.org/yarpc/api/transport"
)

type YarpcHandler struct {
type YARPCHandler struct {
sync.RWMutex

items map[string]string
}

func NewYarpcThriftHandler(service.Host) ([]transport.Procedure, error) {
handler := &YarpcHandler{items: map[string]string{}}
return kvs.New(handler), nil
func NewYARPCThriftHandler(_ service.Host) ([]transport.Procedure, error) {
return kvs.New(&YARPCHandler{items: map[string]string{}}), nil
}

func (h *YarpcHandler) GetValue(ctx context.Context, key *string) (string, error) {
func (h *YARPCHandler) GetValue(ctx context.Context, key *string) (string, error) {
h.RLock()
defer h.RUnlock()

Expand All @@ -51,7 +50,7 @@ func (h *YarpcHandler) GetValue(ctx context.Context, key *string) (string, error
return "", &kv.ResourceDoesNotExist{Key: *key}
}

func (h *YarpcHandler) SetValue(ctx context.Context, key *string, value *string) error {
func (h *YARPCHandler) SetValue(ctx context.Context, key *string, value *string) error {
h.Lock()

h.items[*key] = *value
Expand Down
2 changes: 1 addition & 1 deletion examples/keyvalue/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
svc, err := service.WithModules(
// Create a YARPC module that exposes endpoints
rpc.ThriftModule(
rpc.CreateThriftServiceFunc(NewYarpcThriftHandler),
rpc.CreateThriftServiceFunc(NewYARPCThriftHandler),
modules.WithRoles("service"),
),
).WithOptions(
Expand Down
6 changes: 2 additions & 4 deletions examples/keyvalue/server/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
package main

import (
"context"

"go.uber.org/fx/service"
"go.uber.org/fx/ulog"
"go.uber.org/zap"
)

// Observer receives callbacks during various service lifecycle events
Expand All @@ -37,7 +35,7 @@ type Observer struct {

// OnInit is called during service init process. Returning an error halts the init?
func (o *Observer) OnInit(svc service.Host) error {
ulog.Logger(context.Background()).Info(
zap.S().Info(
"Received service init callback",
"service_name", o.Name(),
"some_number", o.ServiceConfig.SomeNumber,
Expand Down

0 comments on commit f6f8452

Please sign in to comment.