Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pkl/decode_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,14 @@ func getStructFields(typ reflect.Type) map[string]structField {
field := typ.Field(i)
// embedded
if field.Anonymous {
for k, v := range getStructFields(field.Type.Elem()) {
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 {
Comment on lines +308 to +315
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)

ret[k] = v
}
} else {
Expand All @@ -326,7 +333,7 @@ func (d *decoder) getOutputValue(typ reflect.Type) (*reflect.Value, error) {
numFields := typ.NumField()
for i := 0; i < numFields; i++ {
field := typ.Field(i)
if field.Anonymous {
if field.Anonymous && field.Type.Kind() == reflect.Ptr {
fieldValue := reflect.New(field.Type.Elem())
// Assertion: all embedded fields are pointers to structs.
Comment on lines +336 to 338
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.

structValue, err := d.getOutputValue(field.Type.Elem())
Expand Down
17 changes: 17 additions & 0 deletions pkl/test_fixtures/custom/house.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package custom

type Shape struct {
Area int `pkl:"area"`
}

type House struct {
Shape

Bedrooms int `pkl:"bedrooms"`

Bathrooms int `pkl:"bathrooms"`
}

type CustomClasses struct {
House *House `pkl:"house"`
}
31 changes: 31 additions & 0 deletions pkl/test_fixtures/custom/house.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ===----------------------------------------------------------------------===//
module custom

house: House = new {
area = 2000
bedrooms = 3
bathrooms = 2
}

abstract class Shape {
area: Int
}

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

26 changes: 26 additions & 0 deletions pkl/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ package pkl_test

import (
"bytes"
"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

"testing"

unknowntype "github.com/apple/pkl-go/pkl/test_fixtures/gen/unknown_type"
Expand Down Expand Up @@ -83,6 +86,9 @@ var anies []byte
//go:embed test_fixtures/msgpack/unknown_type.pkl.msgpack
var unknownType []byte

//go:embed test_fixtures/custom/house.pkl
var customHousePkl []byte

func TestUnmarshall_Primitives(t *testing.T) {
var res primitives.Primitives
var expected = primitives.Primitives{
Expand Down Expand Up @@ -428,3 +434,23 @@ func TestUnmarshal_UnknownType(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "cannot decode Pkl value of type `PcfRenderer` into Go type `interface {}`. Define a custom mapping for this using `pkl.RegisterMapping`", err.Error())
}

func TestUnmarshal_Custom(t *testing.T) {
evaluator, err := pkl.NewEvaluator(context.Background(), pkl.PreconfiguredOptions)
require.NoError(t, err, "failed to create pkl evaluator")
defer evaluator.Close()

var res custom.CustomClasses
err = evaluator.EvaluateModule(context.Background(), pkl.TextSource(string(customHousePkl)), &res)
require.NoError(t, err, "failed to evaluate pkl module")

assert.Equal(t, custom.CustomClasses{
House: &custom.House{
Shape: custom.Shape{
Area: 2000,
},
Bedrooms: 3,
Bathrooms: 2,
},
}, res)
}