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

Improve the JSON serialization for some AST elements #211

Merged
merged 1 commit into from
Jul 6, 2020

Conversation

turbolent
Copy link
Member

Work towards #210

Start improving the JSON serialization for some AST nodes:

  • Access
  • Argument
  • ConditionKind
  • BoolExpression
  • NilExpression

@turbolent turbolent changed the title improve the JSON serialization for some AST elements Improve the JSON serialization for some AST elements Jul 6, 2020
@turbolent turbolent self-assigned this Jul 6, 2020
@@ -66,10 +67,21 @@ func (e *BoolExpression) String() string {
return "false"
}

func (e *BoolExpression) MarshalJSON() ([]byte, error) {
type Alias BoolExpression
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why you define this type versus just use *BoolExpression below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function tries to add some more properties to the resulting JSON. It could do that by declaring another struct type which has exactly all the same fields like the original, but that would cause a lot of duplication (and maintenance cost).

Marshaling a wrapper struct which embeds the original won't work: the json.Marshal call would in turn call this very same function again for the embedded original, leading to a stack overflow.
This can be prevented by introducing a type alias, which has the same fields as the original, but the MarshallJSON function is not called on it.

For more, see e.g. http://choly.ca/post/go-json-marshalling/

@turbolent turbolent merged commit 8f0dbf9 into master Jul 6, 2020
@turbolent turbolent deleted the bastian/210-ast-json-improvements branch July 6, 2020 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants