-
Notifications
You must be signed in to change notification settings - Fork 24
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
fix: handle pkl sub classes / extending structs in custom classes #33
base: main
Are you sure you want to change the base?
Conversation
add support for unmarshalling into anonymous go struct fields from pkl sub-classes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for letting this sit. One suggestion, at your discretion. Besides that, one critical question (for @bioball).
var fields map[string]structField | ||
switch field.Type.Kind() { | ||
case reflect.Ptr: | ||
fields = getStructFields(field.Type.Elem()) | ||
case reflect.Struct: | ||
fields = getStructFields(field.Type) | ||
} | ||
for k, v := range fields { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not very passionate, but shouldn't the difference be the "smallest"?
var fields map[string]structField | |
switch field.Type.Kind() { | |
case reflect.Ptr: | |
fields = getStructFields(field.Type.Elem()) | |
case reflect.Struct: | |
fields = getStructFields(field.Type) | |
} | |
for k, v := range fields { | |
fieldType := field.Type | |
for fieldType.Kind() == reflect.Ptr { | |
fieldType = fieldType.Elem() | |
} | |
for k, v := range getStructFields(fieldType) { |
(as a side-effect; this also traverses nested pointers)
if field.Anonymous && field.Type.Kind() == reflect.Ptr { | ||
fieldValue := reflect.New(field.Type.Elem()) | ||
// Assertion: all embedded fields are pointers to structs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is now somewhat out of place. Shouldn't there rather be a comment at the top to say no output is written to non-pointer fields? Is it expected to no longer include non-pointer fields in getOutputValue
? (cc @bioball)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just some logic that initializes embedded pointer values, if they exist. If the embedded value is a plain struct, it makes sense that there's nothing to do (the zero value of a struct type also a struct).
I think it's enough to just remove this comment altogether and move on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor nits, but overall looks good!
class House extends Shape { | ||
bedrooms: Int | ||
bathrooms: Int | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename file to custom.pkl
_ "embed" | ||
"github.com/apple/pkl-go/pkl/test_fixtures/custom" | ||
"github.com/stretchr/testify/require" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you run goimports -w .
to update the import order here?
If you don't have goimports, you can run go install golang.org/x/tools/cmd/goimports@latest
to install it.
Details on goimports: https://pkg.go.dev/golang.org/x/tools/cmd/goimports
if field.Anonymous && field.Type.Kind() == reflect.Ptr { | ||
fieldValue := reflect.New(field.Type.Elem()) | ||
// Assertion: all embedded fields are pointers to structs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just some logic that initializes embedded pointer values, if they exist. If the embedded value is a plain struct, it makes sense that there's nothing to do (the zero value of a struct type also a struct).
I think it's enough to just remove this comment altogether and move on.
add support for unmarshalling into anonymous go struct fields from pkl sub-classes
so we can create custom go structs using inheritence and unmarshal pkl to them