From adde408382ed29ba78233eb2d8844e6c0affb1a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D?= Date: Thu, 4 Jul 2019 11:26:46 +0800 Subject: [PATCH 1/3] feat: paladin support json & yaml --- go.mod | 1 + go.sum | 4 ++++ pkg/conf/paladin/value.go | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/go.mod b/go.mod index 7a89bd721..9f89fcc75 100644 --- a/go.mod +++ b/go.mod @@ -36,6 +36,7 @@ require ( google.golang.org/grpc v1.20.1 gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/go-playground/validator.v9 v9.26.0 + gopkg.in/yaml.v2 v2.2.2 ) replace ( diff --git a/go.sum b/go.sum index 8b34b7ceb..bede908d8 100644 --- a/go.sum +++ b/go.sum @@ -101,8 +101,12 @@ github.com/tsuna/gohbase v0.0.0-20190201102810-d3184c1526df h1:jYiwqXfoRWU6pJMzC github.com/tsuna/gohbase v0.0.0-20190201102810-d3184c1526df/go.mod h1:3HfLQly3YNLGxNv/2YOfmz30vcjG9hbuME1GpxoLlGs= github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v9 v9.26.0 h1:2NPPsBpD0ZoxshmLWewQru8rWmbT5JqSzz9D1ZrAjYQ= gopkg.in/go-playground/validator.v9 v9.26.0/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/pkg/conf/paladin/value.go b/pkg/conf/paladin/value.go index ee6dd3db2..6a91c6e61 100644 --- a/pkg/conf/paladin/value.go +++ b/pkg/conf/paladin/value.go @@ -2,11 +2,13 @@ package paladin import ( "encoding" + "encoding/json" "reflect" "time" "github.com/BurntSushi/toml" "github.com/pkg/errors" + yaml "gopkg.in/yaml.v2" ) // ErrNotExist value key not exist. @@ -155,3 +157,20 @@ func (v *Value) UnmarshalTOML(dst interface{}) error { } return toml.Unmarshal([]byte(text), dst) } + +// UnmarshalJSON unmarhsal json to struct. +func (v *Value) UnmarshalJson(dst interface{}) error { + text, err := v.Raw() + if err != nil { + return err + } + return json.Unmarshal([]byte(text), dst) +} + +func (v *Value) UnmarshalYaml(dst interface{}) error { + text, err := v.Raw() + if err != nil { + return err + } + return yaml.Unmarshal([]byte(text), dst) +} From a40baeb8bca817a332da1a91987b19bdc7c04756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D?= Date: Thu, 4 Jul 2019 14:13:48 +0800 Subject: [PATCH 2/3] fix: method name --- pkg/conf/paladin/value.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/conf/paladin/value.go b/pkg/conf/paladin/value.go index 6a91c6e61..bafb86b99 100644 --- a/pkg/conf/paladin/value.go +++ b/pkg/conf/paladin/value.go @@ -159,7 +159,7 @@ func (v *Value) UnmarshalTOML(dst interface{}) error { } // UnmarshalJSON unmarhsal json to struct. -func (v *Value) UnmarshalJson(dst interface{}) error { +func (v *Value) UnmarshalJSON(dst interface{}) error { text, err := v.Raw() if err != nil { return err @@ -167,7 +167,7 @@ func (v *Value) UnmarshalJson(dst interface{}) error { return json.Unmarshal([]byte(text), dst) } -func (v *Value) UnmarshalYaml(dst interface{}) error { +func (v *Value) UnmarshalYAML(dst interface{}) error { text, err := v.Raw() if err != nil { return err From 45943e788ca8bea197c03fcdcab6561d26cf65fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D?= Date: Fri, 5 Jul 2019 16:15:48 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=20=E8=8E=B7=E5=8F=96hostname?= =?UTF-8?q?=E7=9A=84=E4=BC=98=E5=85=88=E7=BA=A7=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/conf/env/env.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/conf/env/env.go b/pkg/conf/env/env.go index 54b867e6d..c8cb6760d 100644 --- a/pkg/conf/env/env.go +++ b/pkg/conf/env/env.go @@ -45,8 +45,12 @@ var ( func init() { var err error - if Hostname, err = os.Hostname(); err != nil || Hostname == "" { - Hostname = os.Getenv("HOSTNAME") + Hostname = os.Getenv("HOSTNAME") + if Hostname == "" { + Hostname, err = os.Hostname() + if err != nil { + Hostname = "" + } } addFlag(flag.CommandLine)