Skip to content

Commit

Permalink
remove empty proto or path when the size is greater than 1 (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 authored Jan 5, 2020
1 parent 1757dad commit 3c0a83d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
13 changes: 10 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,22 @@ func initConfig(fs *pflag.FlagSet) (cfg *Config, err error) {
}

func setupConfig(c *Config) {
// To show protofile and protopath field, set slice which has empty string
// To show protofile and protopath field in a config file, set slice which has empty string
// if these are nil. (please see default values.)
// Conversely, trim the empty string element when config loading.
if (c.Default.ProtoFile == nil) || (len(c.Default.ProtoFile) == 1 && c.Default.ProtoFile[0] == "") {
if c.Default.ProtoFile == nil {
c.Default.ProtoFile = []string{}
}
if (c.Default.ProtoPath == nil) || (len(c.Default.ProtoPath) == 1 && c.Default.ProtoPath[0] == "") {
if len(c.Default.ProtoFile) >= 1 && c.Default.ProtoFile[0] == "" {
c.Default.ProtoFile = c.Default.ProtoFile[1:]
}

if c.Default.ProtoPath == nil {
c.Default.ProtoPath = []string{}
}
if len(c.Default.ProtoPath) >= 1 && c.Default.ProtoPath[0] == "" {
c.Default.ProtoPath = c.Default.ProtoPath[1:]
}
}

// Edit opens the project local config file with an editor.
Expand Down
23 changes: 19 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ func TestMain(m *testing.M) {

func TestLoad(t *testing.T) {
checkValues := func(t *testing.T, c *Config) {
if len(c.Default.ProtoFile) == 1 {
if len(c.Default.ProtoFile) >= 1 {
if len(c.Default.ProtoFile[0]) == 0 {
t.Fatalf("Default.ProtoFile must not empty")
t.Fatalf("Default.ProtoFile must not be empty")
}
}
if len(c.Default.ProtoPath) == 1 {
if len(c.Default.ProtoPath) >= 1 {
if len(c.Default.ProtoPath[0]) == 0 {
t.Fatalf("Default.ProtoPath must not empty")
t.Fatalf("Default.ProtoPath must not be empty")
}
}
}
Expand Down Expand Up @@ -294,6 +294,21 @@ func TestLoad(t *testing.T) {

return cfg
})

assertWithGolden(t, "apply some proto files and paths", func(t *testing.T) *Config {
_, _, cleanup := setupEnv(t)
defer cleanup()

fs := pflag.NewFlagSet("test", pflag.ExitOnError)
fs.StringSlice("path", []string{"foo", "bar"}, "")
fs.StringSlice("proto", []string{"hoge", "fuga"}, "")

cfg := mustGet(t, fs)

checkValues(t, cfg)

return cfg
})
}

func TestEdit(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

[default]
package = ""
protofile = []
protopath = ["foo","bar"]
service = ""

[log]
prefix = "evans: "

[meta]
autoupdate = false
configversion = "0.6.10"
updatelevel = "patch"

[repl]
coloredoutput = true
historysize = 100
inputpromptformat = "{ancestor}{name} ({type}) => "
promptformat = "{package}.{service}@{addr}:{port}"
silent = false
splashtextpath = ""

[request]
cacertfile = ""
certfile = ""
certkeyfile = ""
web = false

[request.header]
grpc-client = ["evans"]

[server]
host = "127.0.0.1"
name = ""
port = "50051"
reflection = false
tls = false

0 comments on commit 3c0a83d

Please sign in to comment.