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

feat: add getJson and outputJson for plugin io util #25

Merged
merged 6 commits into from
Jan 5, 2024
Merged

Conversation

nilslice
Copy link
Member

@nilslice nilslice commented Jan 4, 2024

Looks like zig@master changed some build APIs, so i'll figure that out and update this PR (and likely the SDK repo too)

Comment on lines +56 to +57
const FromJson = Json(T);
const input = FromJson{ .parsed = out, .slice = bytes };
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know of a way to better handle this such that the end-user can get the automatic type from the JSON, and have enough control over freeing the memory so it doesn't deallocate before a value is used.

In the example here:

const Input = struct {
    name: []const u8,
    age: u16,
};

export fn json_input() i32 {
    const plugin = Plugin.init(allocator);
    const json = plugin.getJson(Input, .{}) catch unreachable; // you must call .deinit() when done
    defer json.deinit();

    const input: Input = json.value();
    const out = std.fmt.allocPrint(allocator, "Hello, {s}. You are {d} years old!\n", .{ input.name, input.age }) catch unreachable;

    plugin.output(out);
    return 0;
}

The input.name (a []const u8) would be freed unless json.deinit() is called after this function returns and the value is used outside this function. Initially I thought I could handle this for the end-user, but I don't think I can.

So, this returns the Json wrapped type, with methods .value() (to get the parsed struct), and .deinit() to give the end-user the ability to free the slice used to parse the struct.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can avoid this by using alloc_always when parsing the JSON:

std.json.parseFromSlice(Config, allocator, data, .{.allocate = .alloc_always})

Copy link
Member Author

Choose a reason for hiding this comment

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

@zshipko thanks, this works great!

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, now there is a getJson which returns the comptime T and handles the memory management. kind of the "expected / easy-mode" path.

and, there's a getJsonOpt which is the previous implementation and allows the end user to control the parsing and allocation.

@nilslice nilslice requested review from usdogu and zshipko January 4, 2024 21:30
Copy link
Contributor

@zshipko zshipko left a comment

Choose a reason for hiding this comment

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

Looks good!

@nilslice
Copy link
Member Author

nilslice commented Jan 4, 2024

Going to update the README to match these changes (and today's updates to Zig 😆) and will merge.

@nilslice nilslice merged commit 3f59e07 into main Jan 5, 2024
2 checks passed
@nilslice nilslice deleted the json-util branch January 5, 2024 00:26
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.

2 participants