From c74a3aa90a9316a4d36f7767d04ba2a0bf6965a4 Mon Sep 17 00:00:00 2001 From: Aman Mangal Date: Wed, 16 Aug 2023 19:56:51 +0530 Subject: [PATCH] fix(alpha): convert numbers correctly in superflags (#7712) (#8943) --- dgraph/cmd/root.go | 8 +++++ dgraph/cmd/root_test.go | 76 ++++++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/dgraph/cmd/root.go b/dgraph/cmd/root.go index 26e885b95b1..c1938b7f236 100644 --- a/dgraph/cmd/root.go +++ b/dgraph/cmd/root.go @@ -24,6 +24,7 @@ import ( "io" "os" "path/filepath" + "strconv" "strings" "unicode" @@ -304,6 +305,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] diff --git a/dgraph/cmd/root_test.go b/dgraph/cmd/root_test.go index 19c450fded2..016b5e61c44 100644 --- a/dgraph/cmd/root_test.go +++ b/dgraph/cmd/root_test.go @@ -1,47 +1,53 @@ package cmd import ( - "fmt" + "encoding/json" "io" "strings" "testing" + + "github.com/dgraph-io/ristretto/z" + "github.com/stretchr/testify/require" ) func TestConvertJSON(t *testing.T) { - hier := `{ - "mutations": "strict", - "badger": { - "compression": "zstd:1", - "goroutines": 5 - }, - "raft": { - "idx": 2, - "learner": true - }, - "security": { - "whitelist": "127.0.0.1,0.0.0.0" - } - }` - conv, err := io.ReadAll(convertJSON(hier)) - if err != nil { - t.Fatal("error reading from convertJSON") - } - unchanged, err := io.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; goroutines=5;") && - !strings.Contains(string(conv), "goroutines=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") - } + config := `{ + "mutations": "strict", + "badger": { + "compression": "zstd:1", + "numgoroutines": 5 + }, + "limit": { + "query_edge": 1000000 + }, + "raft": { + "idx": 2, + "learner": true + }, + "security": { + "whitelist": "127.0.0.1,0.0.0.0" + } + }` + + 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) {