Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(flags): Convert numbers correctly in SuperFlags #7712

Merged
merged 1 commit into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dgraph/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"

Expand Down Expand Up @@ -306,6 +307,13 @@ func convertJSON(old string) io.Reader {
// condense superflags
for f, options := range super {
for k, v := range options {
// JSON does not have distinct types for integers and floats.
// Go will always give us a float64 value. So, an exceptionally
// large integer like 1_000_000 will be printed as 1e06 unless
// we format it carefully.
if vFloat, ok := v.(float64); ok {
v = strconv.FormatFloat(vFloat, 'f', -1, 64)
}
good[f] += fmt.Sprintf("%s=%v; ", k, v)
}
good[f] = good[f][:len(good[f])-1]
Expand Down
50 changes: 28 additions & 22 deletions dgraph/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package cmd

import (
"fmt"
"encoding/json"
"io/ioutil"
"strings"
"testing"

"github.com/dgraph-io/ristretto/z"
"github.com/stretchr/testify/require"
)

func TestConvertJSON(t *testing.T) {
hier := `{
config := `{
"mutations": "strict",
"badger": {
"compression": "zstd:1",
"numgoroutines": 5
},
"limit": {
"query_edge": 1000000
},
"raft": {
"idx": 2,
"learner": true
Expand All @@ -22,26 +28,26 @@ func TestConvertJSON(t *testing.T) {
"whitelist": "127.0.0.1,0.0.0.0"
}
}`
conv, err := ioutil.ReadAll(convertJSON(hier))
if err != nil {
t.Fatal("error reading from convertJSON")
}
unchanged, err := ioutil.ReadAll(convertJSON(string(conv)))
if err != nil {
t.Fatal("error reading from convertJSON")
}
if string(unchanged) != string(conv) {
t.Fatal("convertJSON mutating already flattened string")
}
// need both permutations because convertJSON iterates through Go hashmaps in undefined order
if (!strings.Contains(string(conv), "compression=zstd:1; numgoroutines=5;") &&
!strings.Contains(string(conv), "numgoroutines=5; compression=zstd:1;")) ||
(!strings.Contains(string(conv), "idx=2; learner=true;") &&
!strings.Contains(string(conv), "learner=true; idx=2;")) ||
!strings.Contains(string(conv), "whitelist=127.0.0.1,0.0.0.0") {
fmt.Println(string(conv))
t.Fatal("convertJSON not converting properly")
}

var converted map[string]string
err := json.NewDecoder(convertJSON(config)).Decode(&converted)
require.NoError(t, err)

require.Equal(t, "strict", converted["mutations"])

badger := z.NewSuperFlag(converted["badger"])
require.Equal(t, "zstd:1", badger.GetString("compression"))
require.Equal(t, int64(5), badger.GetInt64("numgoroutines"))

limit := z.NewSuperFlag(converted["limit"])
require.Equal(t, int64(1000000), limit.GetInt64("query-edge"))

raft := z.NewSuperFlag(converted["raft"])
require.Equal(t, int64(2), raft.GetInt64("idx"))
require.Equal(t, true, raft.GetBool("learner"))

security := z.NewSuperFlag(converted["security"])
require.Equal(t, "127.0.0.1,0.0.0.0", security.GetString("whitelist"))
}

func TestConvertYAML(t *testing.T) {
Expand Down