Skip to content

Conversation

@jstrachan
Copy link

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

hyperion-jenkins added 2 commits March 8, 2024 04:13
add support for unmarshalling into anonymous go struct fields from pkl sub-classes
Copy link
Collaborator

@holzensp holzensp left a 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).

Comment on lines +308 to +315
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 {
Copy link
Collaborator

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"?

Suggested change
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)

Comment on lines +336 to 338
if field.Anonymous && field.Type.Kind() == reflect.Ptr {
fieldValue := reflect.New(field.Type.Elem())
// Assertion: all embedded fields are pointers to structs.
Copy link
Collaborator

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)

Copy link
Member

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.

Copy link
Member

@bioball bioball left a 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
}
Copy link
Member

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

"context"
_ "embed"
"github.com/apple/pkl-go/pkl/test_fixtures/custom"
"github.com/stretchr/testify/require"
Copy link
Member

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

Comment on lines +336 to 338
if field.Anonymous && field.Type.Kind() == reflect.Ptr {
fieldValue := reflect.New(field.Type.Elem())
// Assertion: all embedded fields are pointers to structs.
Copy link
Member

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.

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