open-feature / flagd

A feature flag daemon with a Unix philosophy
https://openfeature.dev
Apache License 2.0
569 stars 67 forks source link

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

Closed vadasambar closed 1 year ago

vadasambar commented 1 year ago

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/cobra@v1.4.0/command.go:860\ngithub.com/spf13/cobra.(*Command).ExecuteC\n\t/home/suraj/go/pkg/mod/github.com/spf13/cobra@v1.4.0/command.go:974\ngithub.com/spf13/cobra.(*Command).Execute\n\t/home/suraj/go/pkg/mod/github.com/spf13/cobra@v1.4.0/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.

vadasambar commented 1 year ago

I am not sure if we want to fix this now or even fix this at all because we ran into this issue when we tried converting yaml to json (we might not have to do this anytime soon in the future) and we have a way to work around the issue using json.MarshalIndent instead of json.Marshal. I have created this issue for future reference in case anyone runs into a similar issue in the future.