Skip to content

Conversation

@bioball
Copy link
Member

@bioball bioball commented May 1, 2025

Thanks to @thomaspurchas for their contributions here!

This is based on top of #92.

Closes #92

This pull request changes the Go code generator to produce non-pointer types for nullables.
The problem this aims to solve is to address an issue where pointers are used both to represent values that are nullable, and values that are objects. This causes ambiguity (did this come from a nullable type in Pkl?)

Users that prefer the old style for whatever reason can still use an older version of pkl-gen-go.
The decoder determines the correct type based on whether the declared type is a pointer or not.

@bioball bioball force-pushed the concrete-structs branch from e9e9c18 to 6660697 Compare May 1, 2025 19:15
@jalius
Copy link

jalius commented Jul 3, 2025

This is good. We can now trivially copy structs generated by pkl-gen-go (assuming no nullable properties). Any timeline for when this will be merged?

Copy link
Contributor

@stackoverflow stackoverflow left a comment

Choose a reason for hiding this comment

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

LGTM.

thomaspurchas and others added 6 commits July 16, 2025 16:49
Normal go conventions encourage the returning on structs rather than
pointers or interfaces. It's also normal convension to make optional
values pointers, and non-optional concrete types, including stucts,
rather than pointers to structs.

As it very easy for a someone to choose to take a pointer to a struct in
go and pass that around if they want to avoid copies (although the go
compiler is heavily optimised to support passing complete structs, even
large ones, around), there's little benifit in making embeded structs
pointers. It just requires that downstream code needs to be filled with
annoying nil checkes, and generally makes it harder to write robust,
clean code.
Codegen changes means that the old test fixtures are out of date, and
can't be used to test the go binding code. Updating these fixtures
breaks the binding code, and identifies the areas that need enhancement.
The codegen changes means we need to support decoding data into concrete
structs, and not just pointers to structs.
* Revert rename-based changes (no I-prefix for interfaces, structs have Impl postfix)
* Change built-in types to be non-pointer types too (pkl.Duration vs *pkl.Duration)
* Detect whether `RegisterMapping` receives a pointer vs non-pointer (this is a breaking change)
* Change `GeneratorSettings.pkl.go` to use non-pointer types
@bioball bioball force-pushed the concrete-structs branch from e480624 to 5ac4228 Compare July 16, 2025 23:50
@bioball bioball merged commit a7d9e78 into apple:main Jul 17, 2025
7 checks passed
@bioball bioball deleted the concrete-structs branch July 17, 2025 00:02
@bioball
Copy link
Member Author

bioball commented Jul 17, 2025

@jalius this is merged, and we'll aim to release a new version of pkl-go sometime in the next two weeks!

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