Skip to content

[BUG] flagd (transposeEvaluators fn) doesn't understand json without indentations #244

@vadasambar

Description

@vadasambar

Observed behavior

transposeEvaluators doesn't behave correctly if the json passed to it doesn't have the right indentation.

Expected Behavior

transposeEvaluators should handle cases where the json passed to it doesn't have the right indentation.

Steps to reproduce

  1. Check out this PR's branch
  2. Put fmt.Println statements just after this line and after this line.
  3. Use json.Marshal instead of json.MarshalIndent here
  4. From flagd root directory run (example_flags.yaml can be found here):
$ go run main.go  start -f file:./config/samples/example_flags.yaml -e=yaml 
{"level":"info","ts":1672807996.7619019,"caller":"service/connect_service.go:108","msg":"metrics listening at 8014","component":"service"}
evalValue before {"in":["@faas.com",{"var":["email"]}]}
evalValue after "in":["@faas.com",{"var":["email"]}
{"level":"fatal","ts":1672807996.762957,"caller":"cmd/start.go:116","msg":"set state: unmarshal new state: invalid character '}' after array element","component":"start","stacktrace":"github.com/open-feature/flagd/cmd.glob..func1\n\t/home/suraj/sandbox/flagd/cmd/start.go:116\ngithub.com/spf13/cobra.(*Command).execute\n\t/home/suraj/go/pkg/mod/github.com/spf13/[email protected]/command.go:860\ngithub.com/spf13/cobra.(*Command).ExecuteC\n\t/home/suraj/go/pkg/mod/github.com/spf13/[email protected]/command.go:974\ngithub.com/spf13/cobra.(*Command).Execute\n\t/home/suraj/go/pkg/mod/github.com/spf13/[email protected]/command.go:902\ngithub.com/open-feature/flagd/cmd.Execute\n\t/home/suraj/sandbox/flagd/cmd/root.go:38\nmain.main\n\t/home/suraj/sandbox/flagd/main.go:30\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:250"}
exit status 1

If you check the above log,

evalValue before {"in":["@faas.com",{"var":["email"]}]}
evalValue after "in":["@faas.com",{"var":["email"]}

Because the trailing brackets are not on newline, flagd trims the last two trailing brackets which makes the json invalid. This problem doesn't happen if you use json.MarshalIndent like this

To contrast this with using a json config file,

$ go run main.go  start -f file:./config/samples/example_flags.json -e=json
{"level":"info","ts":1672808353.423182,"caller":"service/connect_service.go:108","msg":"metrics listening at 8014","component":"service"}
evalValue before {
          "in": ["@faas.com", {
            "var": ["email"]
          }]
    }
evalValue after 
          "in": ["@faas.com", {
            "var": ["email"]
          }]
   
{"level":"info","ts":1672808353.4240851,"caller":"runtime/runtime.go:84","msg":"configuration change (write) for flagKey myBoolFlag (./config/samples/example_flags.json)","component":"runtime"}
...

There is no error. Looking at the log above,

evalValue before {
          "in": ["@faas.com", {
            "var": ["email"]
          }]
    }
evalValue after 
          "in": ["@faas.com", {
            "var": ["email"]
          }]

Notice how only the trailing bracket on newline is removed (with the opening bracket on the first line). Resulting json is valid.

Metadata

Metadata

Labels

Needs TriageThis issue needs to be investigated by a maintainerbugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions