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

jshn.sh: add convenience functions, cleanup #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

pprindeville
Copy link
Member

You might not always want to populate JSON serially, so a way to save/restore the cursor through a convenience function is handy.

Also, adding anonymous (unnamed) strings to an array isn't obvious, so added a convenience function for that.

Lastly, some minor cleanup to show what the parameters are to functions, etc.

@pprindeville
Copy link
Member Author

@nbd168 Can I get a review please? I need some of these changes to help with a migration from ISC-DHCP to Kea.

@pprindeville
Copy link
Member Author

pprindeville commented Nov 4, 2023

cc: @Ansuel @dangowrt @neheb @stintel @jow-

@pprindeville
Copy link
Member Author

@jow- Can you please look this over?

@jow-
Copy link
Contributor

jow- commented Nov 20, 2023

Most (all?) existing functions use reference style returns (caller passes a destination variable name, function populates it and uses it's numeric return value to signify success/error) while your new functions introduce echo style returns requiring calling code to use ugly subshell invocations.

Personally I would also change the naming like that:

  • json_get_pos -> json_get_position
  • json_set_pos -> json_move_to
  • json_get_parent -> json_move_up
  • json_add_anon_string -> json_push_string

@pprindeville
Copy link
Member Author

pprindeville commented Nov 21, 2023

Most (all?) existing functions use reference style returns (caller passes a destination variable name, function populates it and uses it's numeric return value to signify success/error) while your new functions introduce echo style returns requiring calling code to use ugly subshell invocations.

Personally I would also change the naming like that:

  • json_get_pos -> json_get_position
  • json_set_pos -> json_move_to
  • json_get_parent -> json_move_up
  • json_add_anon_string -> json_push_string

Thanks, making those changes now.

By json_get_pos you meant json_get_cur, and that should become json_get_position?

And json_set_cur should become json_move_to? Or json_set_position (for symmetry)?

Where do the functions _get_var and _set_var get used? I can only find references to _json_get_var and _json_set_var...

json_get_parent isn't meant to move the cursor explicitly. It's a way to get the parent of a node if you need to do something like query the parent's type (array, table, etc).

For instance, you could implement json_select ".." as:

    local parent
    json_get_parent parent
    json_set_position "$parent"

if json_select ".." didn't exist.

@pprindeville
Copy link
Member Author

Casting a wider net

cc: @blogic

pprindeville and others added 3 commits April 1, 2024 12:26
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
@pprindeville
Copy link
Member Author

Creating 2nd PR (#12) without the positioning getter/setting commit which seems to be stalled.

@pprindeville
Copy link
Member Author

Most (all?) existing functions use reference style returns (caller passes a destination variable name, function populates it and uses it's numeric return value to signify success/error) while your new functions introduce echo style returns requiring calling code to use ugly subshell invocations.

Personally I would also change the naming like that:

  • json_get_pos -> json_get_position
  • json_set_pos -> json_move_to
  • json_get_parent -> json_move_up
  • json_add_anon_string -> json_push_string

@jow- I believe I've made these changes as requested. Can you please re-review?

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants