Skip to content

Commit

Permalink
Fix JSON encoding adding newlines.
Browse files Browse the repository at this point in the history
This manifested itself when encoding config values, which all map to
strings. An extra new line would get added by json.Encode, which caused
other things to break with confusing error messagges. Switching to
json.Marshal seems to solve the problem.
  • Loading branch information
raskchanky committed May 11, 2020
1 parent 33ee860 commit 6310572
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 7 deletions.
8 changes: 8 additions & 0 deletions command/server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func TestLoadConfigFile_json2(t *testing.T) {
testLoadConfigFile_json2(t, nil)
}

func TestLoadConfigFileIntegerAndBooleanValues(t *testing.T) {
testLoadConfigFileIntegerAndBooleanValues(t)
}

func TestLoadConfigFileIntegerAndBooleanValuesJson(t *testing.T) {
testLoadConfigFileIntegerAndBooleanValuesJson(t)
}

func TestLoadConfigDir(t *testing.T) {
testLoadConfigDir(t)
}
Expand Down
54 changes: 53 additions & 1 deletion command/server/config_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func testConfigRaftRetryJoin(t *testing.T) {
if err != nil {
t.Fatal(err)
}
retryJoinConfig := `[{"leader_api_addr":"http://127.0.0.1:8200"},{"leader_api_addr":"http://127.0.0.2:8200"},{"leader_api_addr":"http://127.0.0.3:8200"}]` + "\n"
retryJoinConfig := `[{"leader_api_addr":"http://127.0.0.1:8200"},{"leader_api_addr":"http://127.0.0.2:8200"},{"leader_api_addr":"http://127.0.0.3:8200"}]`
expected := &Config{
Listeners: []*Listener{
{
Expand Down Expand Up @@ -274,6 +274,58 @@ func testParseEntropy(t *testing.T, oss bool) {
}
}

func testLoadConfigFileIntegerAndBooleanValues(t *testing.T) {
testLoadConfigFileIntegerAndBooleanValuesCommon(t, "./test-fixtures/config4.hcl")
}

func testLoadConfigFileIntegerAndBooleanValuesJson(t *testing.T) {
testLoadConfigFileIntegerAndBooleanValuesCommon(t, "./test-fixtures/config4.hcl.json")
}

func testLoadConfigFileIntegerAndBooleanValuesCommon(t *testing.T, path string) {
config, err := LoadConfigFile(path)
if err != nil {
t.Fatalf("err: %s", err)
}

expected := &Config{
Listeners: []*Listener{
&Listener{
Type: "tcp",
Config: map[string]interface{}{
"address": "127.0.0.1:8200",
},
},
},

Storage: &Storage{
Type: "raft",
Config: map[string]string{
"path": "/storage/path/raft",
"node_id": "raft1",
"performance_multiplier": "1",
"foo": "bar",
"baz": "true",
},
ClusterAddr: "127.0.0.1:8201",
},

ClusterAddr: "127.0.0.1:8201",

DisableCache: true,
DisableCacheRaw: true,
DisableMlock: true,
DisableMlockRaw: true,
EnableUI: true,
EnableUIRaw: true,
}

if !reflect.DeepEqual(config, expected) {
t.Fatalf("expected \n\n%#v\n\n to be \n\n%#v\n\n", config, expected)
}

}

func testLoadConfigFile(t *testing.T) {
config, err := LoadConfigFile("./test-fixtures/config.hcl")
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions command/server/test-fixtures/config4.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
disable_cache = true
disable_mlock = true
ui = true

listener "tcp" {
address = "127.0.0.1:8200"
}

storage "raft" {
path = "/storage/path/raft"
node_id = "raft1"
performance_multiplier = 1
foo = "bar"
baz = true
}

cluster_addr = "127.0.0.1:8201"
20 changes: 20 additions & 0 deletions command/server/test-fixtures/config4.hcl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"disable_cache": true,
"disable_mlock": true,
"ui":true,
"listener": [{
"tcp": {
"address": "127.0.0.1:8200"
}
}],
"storage": {
"raft": {
"path": "/storage/path/raft",
"node_id": "raft1",
"performance_multiplier": 1,
"foo": "bar",
"baz": true
}
},
"cluster_addr": "127.0.0.1:8201"
}
8 changes: 2 additions & 6 deletions sdk/helper/jsonutil/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ func EncodeJSON(in interface{}) ([]byte, error) {
if in == nil {
return nil, fmt.Errorf("input for encoding is nil")
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(in); err != nil {
return nil, err
}
return buf.Bytes(), nil

return json.Marshal(in)
}

// EncodeJSONAndCompress encodes the given input into JSON and compresses the
Expand Down

0 comments on commit 6310572

Please sign in to comment.