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

Reject unknown fields when parsing .yaml files #587

Closed
byorgey opened this issue Jul 24, 2022 · 4 comments
Closed

Reject unknown fields when parsing .yaml files #587

byorgey opened this issue Jul 24, 2022 · 4 comments
Assignees
Labels
C-Low Hanging Fruit Ideal issue for new contributors. L-Parsing Parsing the Swarm language from a string into an AST. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Developer Experience This issue seeks to make life easier for developers writing Scenarios or other Swarm code. Z-Feature A new feature to be added to the game.

Comments

@byorgey
Copy link
Member

byorgey commented Jul 24, 2022

Is your feature request related to a problem? Please describe.
I just spent a frustrating 20 minutes trying to figure out why my scenario didn't work the way I wanted, until I finally realized that the seed: field should be at the top level, rather than indented under world:. No error was generated because (1) the seed is optional, and (2) unknown fields are simply ignored.

Describe the solution you'd like
We should have a standard way to check for, and reject, unknown/misplaced/misspelled fields. For example, see this StackOverflow question: https://stackoverflow.com/questions/67375099/prevent-unknown-field-names-in-aeson-parsejson

@byorgey byorgey added Z-Feature A new feature to be added to the game. C-Low Hanging Fruit Ideal issue for new contributors. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. L-Parsing Parsing the Swarm language from a string into an AST. labels Jul 24, 2022
@xsebek
Copy link
Member

xsebek commented Jul 24, 2022

We could show a Warning on it and ask the player to confirm:

$ swarm --scenario MyScenario.yaml
WARNING: unrecognized field in scenario file (15:2):
- "gaol"
+ did you mean "goal"?
There was 1 warning generated for the scenario file "MyScenario.yaml"!
**Would you like to play it anyway? Type [y]es or [n]o:**
> no please just fix it for me
**Would you like to play it anyway? Type [y]es or [n]o:**
> n

We should check for such warnings in tests of course. 👍

@byorgey
Copy link
Member Author

byorgey commented Jul 24, 2022

Well that is a lot fancier than what I was thinking, but sure, why not. 😄

@byorgey
Copy link
Member Author

byorgey commented Oct 24, 2022

According to the link, for types where the FromJSON instance is derived generically, we can just do something like

instance FromJSON MyType where
  parseJSON = genericParseJSON $ defaultOptions
    { rejectUnknownFields = True }

For other types where we have a custom FromJSON instance, we'll have to do more manual checking. In the linked StackOverflow answer, this happens by checking the keys of the HashMap we get from aeson.

@kostmo kostmo self-assigned this Apr 24, 2023
@kostmo kostmo added the Z-Developer Experience This issue seeks to make life easier for developers writing Scenarios or other Swarm code. label May 30, 2023
@kostmo
Copy link
Member

kostmo commented Sep 10, 2023

This is now implemented: #1475 (comment)

@kostmo kostmo closed this as completed Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Low Hanging Fruit Ideal issue for new contributors. L-Parsing Parsing the Swarm language from a string into an AST. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Developer Experience This issue seeks to make life easier for developers writing Scenarios or other Swarm code. Z-Feature A new feature to be added to the game.
Projects
None yet
Development

No branches or pull requests

3 participants