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

a method to serialize object.Object into a JSON compatible struct #173

Closed
dsikes opened this issue May 15, 2021 · 11 comments · Fixed by #174
Closed

a method to serialize object.Object into a JSON compatible struct #173

dsikes opened this issue May 15, 2021 · 11 comments · Fixed by #174
Labels
enhancement New feature or request language sponsored An issue raised by a sponsor

Comments

@dsikes
Copy link
Contributor

dsikes commented May 15, 2021

With eval.Execute returning the actual object.Object, it would be very useful to have a way to convert the object.Object into a struct that can be serialized as JSON.

A simple eval script can then be used to create JSON views by plucking the data from the object, and returning a different view of the data, like in this sample script:

if (name == "Dan") {
  return {
    "first_name": name,
   "city": address.city,
  };
}

Even though my actual object contains significantly more data, in this particular case, it'd only be a JSON object that returns a first name and a city.

@skx
Copy link
Owner

skx commented May 15, 2021

I think the general case is pretty hard, because I can imagine a script which ended:

  return 3;

That's not something you can return as json unless you changed it to:

  { "return": 3}

That said I think the hash object is a good exception. I could add a JSON interface to allow hashes, specifically, to be converted to json. So your example would work.

(I guess similarly an array-object could be made to work, providing all the members had the same type. So return [3,4,5]; or return [ "foo", "bar", "baz" ];.)

Would hash-only suffice for your use-case?

@skx skx added enhancement New feature or request language sponsored An issue raised by a sponsor labels May 15, 2021
@dsikes
Copy link
Contributor Author

dsikes commented May 15, 2021

Yes, hash only would work. I think that is a reasonable expectation to be fair to your point as well.
If a script returns a non hash, the caller likely wouldn't want to serialize it as JSON directly from the return of the evalfilter.

@skx
Copy link
Owner

skx commented May 15, 2021

Hrm, now I say that of course 3 is valid JSON. As is "Steve".

So I can implement for:

  • Numbers.
  • Strings.
  • Bools.
  • Hashes.
  • Arrays.

That'd probably suffice for most cases.

@dsikes
Copy link
Contributor Author

dsikes commented May 15, 2021

Thats true. Good point. How do you see one serializing the return object of the eval filter as JSON?

@skx
Copy link
Owner

skx commented May 15, 2021

I would probably say you'd write something like this:

 // prepare
 ret, err := eval.Run(blah)
 
 helper, ok := red.(object.ToJSON)
 if !ok {
    panic("object doesn't implement the ToJSON interface");
 }

 byte := helper.ToJSON()

At which point byte would contain the JSON. (Or it could be a string. Unsure.)

@dsikes
Copy link
Contributor Author

dsikes commented May 15, 2021

That LGTM. How difficult is something like that to implement?

@skx
Copy link
Owner

skx commented May 15, 2021

I don't think it would take too long - I'd just need to define the interface and then add the ToJSON method to each object-type beneath objects/.

Handling the case of recursion / hashes will be harder than the string/bool/integers, but even so I think a couple of hours should suffice.

I'll have a stab at it tomorrow/Monday.

@dsikes
Copy link
Contributor Author

dsikes commented May 15, 2021

I accidentally clicked the wrong emoji.. haha -- sounds good. Thanks for your effort.

@skx skx mentioned this issue May 16, 2021
@skx
Copy link
Owner

skx commented May 16, 2021

PR in-progress:

  • Renamed the interface to JSONAble.
  • Return value of JSON is (string, error).
  • Used/Demonstrated in cmd/evalfilter/run_cmd.go

TODO

  • Add tests.
  • Handle escaping characters. e.g. If a string contains " we'd generate bogus JSON right now.

Will come back to it later in the day.

@skx
Copy link
Owner

skx commented May 16, 2021

  • Test-cases added.
  • Quote-handling is good, I think.

Needs more testing/experimentation, but I think it is complete now.

@skx skx closed this as completed in #174 May 16, 2021
@dsikes
Copy link
Contributor Author

dsikes commented May 17, 2021

I'll pull it in and give it a shot. Thanks for knocking it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request language sponsored An issue raised by a sponsor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants