Skip to content

libubox: Add anonymous strings, ints, et al in arrays#12

Merged
openwrt-bot merged 3 commits into
openwrt:masterfrom
pprindeville:separate-fixes-from-new-functionality
Feb 12, 2026
Merged

libubox: Add anonymous strings, ints, et al in arrays#12
openwrt-bot merged 3 commits into
openwrt:masterfrom
pprindeville:separate-fixes-from-new-functionality

Conversation

@pprindeville

@pprindeville pprindeville commented Apr 1, 2024

Copy link
Copy Markdown
Member

You can have a "name" in an array member without an associated value.

@pprindeville

Copy link
Copy Markdown
Member Author

@Ansuel Can someone please review this?

@pprindeville

Copy link
Copy Markdown
Member Author

cc: @robimarko @neheb @hauke @ynezz

@pprindeville

Copy link
Copy Markdown
Member Author

cc: @nbd168

Comment thread sh/jshn.sh
Comment thread sh/jshn.sh
Comment thread sh/jshn.sh
Comment thread sh/jshn.sh
@pprindeville

Copy link
Copy Markdown
Member Author

@i-jonas I thank you for taking the time to diligently review this.

@pprindeville
pprindeville requested a review from l-jonas November 11, 2025 20:26
@pprindeville
pprindeville force-pushed the separate-fixes-from-new-functionality branch 3 times, most recently from 31a6f71 to 3096a5b Compare November 12, 2025 00:54
@pprindeville pprindeville changed the title jshn: Usability improvements libubox: Usability improvements Nov 12, 2025
@pprindeville
pprindeville force-pushed the separate-fixes-from-new-functionality branch from 3096a5b to 83cd4c7 Compare November 12, 2025 02:40
@l-jonas

l-jonas commented Nov 12, 2025

Copy link
Copy Markdown

It looks like json_push_str and json_push_int are not necessary at all. I found the documentation at https://openwrt.org/docs/guide-developer/jshn.

. /usr/share/libubox/jshn.sh
json_init
json_add_array test
json_add_int "" 1
json_add_int "" 2
json_add_int "" 3
json_add_string "" "eins"
json_add_string "" "zwei"
json_add_string "" "drei"
json_close_array
json_dump

just results in { "test": [ 1, 2, 3, "eins", "zwei", "drei" ] }

@pprindeville

pprindeville commented Nov 12, 2025

Copy link
Copy Markdown
Member Author

It looks like json_push_str and json_push_int are not necessary at all. I found the documentation at https://openwrt.org/docs/guide-developer/jshn.

. /usr/share/libubox/jshn.sh
json_init
json_add_array test
json_add_int "" 1
json_add_int "" 2
json_add_int "" 3
json_add_string "" "eins"
json_add_string "" "zwei"
json_add_string "" "drei"
json_close_array
json_dump

just results in { "test": [ 1, 2, 3, "eins", "zwei", "drei" ] }

And that's usually the point of convenience functions. They provide a simplified wrapper for doing something else.

As I point out elsewhere, though, there are packages like netifd which provide their own version of the same convenience function: I'd rather collect all of these in one place rather than reinventing the wheel. There's no cost to having extra convenience functions... it's interpreted code so no object bloat. And it makes the code easier to read.

@l-jonas

l-jonas commented Nov 13, 2025

Copy link
Copy Markdown

They provide a simplified wrapper fro doing something else.

Why are they not implemented as wrappers around the existing functions in the public API like your referenced code examples do it?

Why are the new warnings not implemented in a way that code not using the new convenience functions also benefits from it?

their own version of the same convenience function

Most of your referenced source code parts are domain specific convenience functions that cannot be compared with that. Those that are wrappers are implemented better than your suggestion, e.g. by allowing to add multiple array elements at once. Why does your convenience function not support that?

it's interpreted code so no object bloat

Instead, its source code bloat. I don't see a difference here.

And it makes the code easier to read.

For me, it does not make a difference.

@pprindeville

Copy link
Copy Markdown
Member Author

They provide a simplified wrapper fro doing something else.

Why are they not implemented as wrappers around the existing functions in the public API like your referenced code examples do it?

No idea. Perhaps they also gave up on making changes to libubox?

Why are the new warnings not implemented in a way that code not using the new convenience functions also benefits from it?

Sorry, how do you mean? How would that work?

their own version of the same convenience function

Most of your referenced source code parts are domain specific convenience functions that cannot be compared with that. Those that are wrappers are implemented better than your suggestion, e.g. by allowing to add multiple array elements at once. Why does your convenience function not support that?

Disagree. My proposed json_push_string does what _proto_push_string but with checking to make sure it's happening within an array:

https://github.com/openwrt/netifd/blob/master/scripts/netifd-proto.sh#L240-L242

it's interpreted code so no object bloat

Instead, its source code bloat. I don't see a difference here.

Then what... we stop adding any new code at all?

And it makes the code easier to read.

For me, it does not make a difference.

How much are you having to write scripting with jshn? I've had to jump into it and it's more of a lift than it should be.

@l-jonas

l-jonas commented Nov 13, 2025

Copy link
Copy Markdown

No idea. Perhaps they also gave up on making changes to libubox?

I was saying that the examples you referenced implemented it better than you from my perspective. Now I was asking why you implement it differently?

Sorry, how do you mean? How would that work?

Take a look at that other implementations. Then think about the call stack.

Disagree.

Disagree with the disagree. The check can be added at another place where it affects all calls, including those using the current public API. See previous point.

e.g. by allowing to add multiple array elements at once

Please check the links you provided again. This feels to me like an AI research result that throws links without understanding their content.

Then what... we stop adding any new code at all?

No. I just dislike the argument "no bloat because not compiled".

I've had to jump into it and it's more of a lift than it should be.

Using another function name to skip one empty argument? This particular thing is not that helpful. I've worked with JSON in OpenWrt in shell scripts and really liked replacing that with ucode.

@pprindeville

Copy link
Copy Markdown
Member Author

No idea. Perhaps they also gave up on making changes to libubox?

I was saying that the examples you referenced implemented it better than you from my perspective. Now I was asking why you implement it differently?

All my examples are calling json_add_string "" arg... is that what we're talking about? And you're saying that's better than json_push_string arg as I'm proposing?

Sorry, how do you mean? How would that work?

Take a look at that other implementations. Then think about the call stack.

I've lost the thread. Which other implementations? Can you post a link?

Disagree.

Disagree with the disagree. The check can be added at another place where it affects all calls, including those using the current public API. See previous point.

But... that was the point of adding a wrapper called json_push_string that explicitly tests for being inside an array... which simply chaining to json_add_string "" "$1" wouldn't do.

e.g. by allowing to add multiple array elements at once

Please check the links you provided again. This feels to me like an AI research result that throws links without understanding their content.

Are we talking about the links mentioned here?

#12 (comment)

I grepped through the sources and manually copy & pasted those links.

Then what... we stop adding any new code at all?

No. I just dislike the argument "no bloat because not compiled".

Any new functionality comes with some cost unless it includes serious refactoring to shrink overall size. Even there the cost is risk of introducing new bugs or regressions.

I've had to jump into it and it's more of a lift than it should be.

Using another function name to skip one empty argument? This particular thing is not that helpful. I've worked with JSON in OpenWrt in shell scripts and really liked replacing that with ucode.

Perhaps but I like having commonality between ISC-DHCP and Kea. Makes it easier for me.

@l-jonas

l-jonas commented Nov 14, 2025

Copy link
Copy Markdown

All my examples are calling json_add_string "" arg... is that what we're talking about? And you're saying that's better than json_push_string arg as I'm proposing?

Yes

But... that was the point of adding a wrapper called json_push_string that explicitly tests for being inside an array... which simply chaining to json_add_string "" "$1" wouldn't do.

json_add_string could do some check depending on the first argument empty or not. So you could warn for keys in an array and no keys in an object. Or maybe _json_add_generic to make it independent from string or integer.

I grepped through the sources and manually copy & pasted those links.

I believe that and still made some experiences with junior devs. That's why it feels like that. I was referring to https://github.com/openwrt/openwrt/blob/main/package/system/procd/files/procd.sh#L114 which adds some value over a simple wrapper.

Any new functionality comes with some cost unless it includes serious refactoring to shrink overall size. Even there the cost is risk of introducing new bugs or regressions.

I know. And due to this, one has to balance the advantages and the risks.

Perhaps but I like having commonality between ISC-DHCP and Kea. Makes it easier for me.

How is this related to one isolated script converting the configuration?

@pprindeville

Copy link
Copy Markdown
Member Author

Perhaps but I like having commonality between ISC-DHCP and Kea. Makes it easier for me.

How is this related to one isolated script converting the configuration?

It shares a lot of code with dhcpd.init: the validation of the UCI, the synthesis of the resources records and integration with Bind is all the same. All that's different is the generation of the DHCP configuration target file itself.

@pprindeville

Copy link
Copy Markdown
Member Author

Separating out json_get_index in case that alone is palatable. See PR #33.

@l-jonas

l-jonas commented Nov 14, 2025

Copy link
Copy Markdown

It shares a lot of code with dhcpd.init

Sharing by duplicating? If there would be a third package like dhcp-common containing the shared code, then I would call and consider it sharing.

@pprindeville

Copy link
Copy Markdown
Member Author

It shares a lot of code with dhcpd.init

Sharing by duplicating? If there would be a third package like dhcp-common containing the shared code, then I would call and consider it sharing.

But they'll never both be installed at the same time.

@l-jonas

l-jonas commented Nov 14, 2025

Copy link
Copy Markdown

But they'll never both be installed at the same time.

I'm thinking about the source code duplication. You could try to use a symlink within this repository to actually share code between to packages, but the common package sounds cleaner.

@pprindeville
pprindeville force-pushed the separate-fixes-from-new-functionality branch 2 times, most recently from 2cff3b7 to 9223a34 Compare November 27, 2025 21:23
@pprindeville pprindeville changed the title libubox: Usability improvements libubox: Add anonymous strings, ints, et al in arrays Nov 27, 2025
@pprindeville

Copy link
Copy Markdown
Member Author

Added test coverage and complete scalars

cc: @nbd168 @jow- @systemcrash @Ansuel

@pprindeville
pprindeville force-pushed the separate-fixes-from-new-functionality branch from 9223a34 to 946915c Compare December 13, 2025 03:22
@pprindeville
pprindeville force-pushed the separate-fixes-from-new-functionality branch from 946915c to e5b9856 Compare February 3, 2026 04:30
@pprindeville
pprindeville force-pushed the separate-fixes-from-new-functionality branch from e5b9856 to 0b966a5 Compare February 3, 2026 18:39
@pprindeville

Copy link
Copy Markdown
Member Author

cc: @Noltari

@pprindeville

Copy link
Copy Markdown
Member Author

Anyone?

Errors are sent to stderr in all but once place.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Link: openwrt#12
Signed-off-by: Robert Marko <robimarko@gmail.com>
Use local variables for positional arguments where they're captured,
and provide comments for position arguments.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Link: openwrt#12
Signed-off-by: Robert Marko <robimarko@gmail.com>
You can have a "name" in an array member without an associated value.

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
Link: openwrt#12
Signed-off-by: Robert Marko <robimarko@gmail.com>
@robimarko
robimarko force-pushed the separate-fixes-from-new-functionality branch from 0b966a5 to 8c7b489 Compare February 12, 2026 15:57
@robimarko

Copy link
Copy Markdown

Thanks! Rebased on top of master and merged!

@openwrt-bot
openwrt-bot merged commit 8c7b489 into openwrt:master Feb 12, 2026
@pprindeville
pprindeville deleted the separate-fixes-from-new-functionality branch February 14, 2026 04:41
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.

4 participants