From 8e20e65dfa7aeca0ae120a3cf95d135cf101cc4d Mon Sep 17 00:00:00 2001 From: Thomas Purchas Date: Sun, 30 Jun 2024 21:08:49 +0100 Subject: [PATCH 1/4] Change generated go code to follow go conventions 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. Finally rather than having interfaces and "Impl" structs, this change swaps those around. Structs don't have any postfix at all, and interfaces now have an I prefix. The big advantage of this approach is that it makes struct naming much more stable. Previously making a Pkl class open or abstract would result in the generated structs changing their names include "Impl", and the original names becoming interfaces. In turn breaking any code already using the existing structs. Now those breakages won't occur, and devs can opt into using the newly generated interfaces as needed, rather than having to re-write existing code to support the change. --- codegen/src/internal/ClassGen.pkl | 42 +++++++++++++++++++++++------- codegen/src/internal/GoMapping.pkl | 10 +++---- codegen/src/internal/typegen.pkl | 12 ++++----- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/codegen/src/internal/ClassGen.pkl b/codegen/src/internal/ClassGen.pkl index 8f5173d..f855ced 100644 --- a/codegen/src/internal/ClassGen.pkl +++ b/codegen/src/internal/ClassGen.pkl @@ -42,7 +42,7 @@ contents = new Listing { interface when (!isAbstract) { "" - "var _ \(classInfo.interface.name) = (\(classInfo.struct.type.render(classInfo.goPackage)))(nil)" + "var _ \(classInfo.interface.name) = \(classInfo.struct.type.render(classInfo.goPackage)){}" } } when (!isAbstract) { @@ -55,11 +55,11 @@ contents = new Listing { } when (isModule && !isAbstract) { "" - "// LoadFromPath loads the pkl module at the given path and evaluates it into a \(classInfo.name)" - "func LoadFromPath(ctx context.Context, path string) (ret \(classInfo.type.render(classInfo.goPackage)), err error) {" + "// LoadFromPath loads the pkl module at the given path and evaluates it into a \(classInfo.struct.name)" + "func LoadFromPath(ctx context.Context, path string) (ret \(classInfo.struct.type.render(classInfo.goPackage)), err error) {" "\tevaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions)" "\tif err != nil {" - "\t\treturn nil, err" + "\t\treturn \(classInfo.struct.type.render(classInfo.goPackage)){}, err" "\t}" "\tdefer func() {" "\t\tcerr := evaluator.Close()" @@ -71,13 +71,13 @@ contents = new Listing { "\treturn ret, err" "}" "" - "// Load loads the pkl module at the given source and evaluates it with the given evaluator into a \(classInfo.name)" - "func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (\(classInfo.type.render(classInfo.goPackage)), error) {" + "// Load loads the pkl module at the given source and evaluates it with the given evaluator into a \(classInfo.struct.name)" + "func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (\(classInfo.struct.type.render(classInfo.goPackage)), error) {" "\tvar ret \(classInfo.struct.name)" "\tif err := evaluator.EvaluateModule(ctx, source, &ret); err != nil {" - "\t\treturn nil, err" + "\t\treturn \(classInfo.struct.name){}, err" "\t}" - "\treturn &ret, nil" + "\treturn ret, nil" "}" } "" @@ -186,7 +186,15 @@ local struct: String = new Listing { when (field.docComment != null) { utils.renderDocComment(field.docComment!!, "\t") } - renderStructField(pklPropertyName, field) + when (field.type == classInfo.type) { + renderStructField(pklPropertyName, (field) { + type = new Type.Pointer { + elem = field.type + } + }) + } else { + renderStructField(pklPropertyName, field) + } } "}" } @@ -200,7 +208,7 @@ local interface: String? = new Listing { "type \(classInfo.interface.name) interface {" when (superClass != null) { - "\t\(superClass.type.render(classInfo.goPackage))" + "\t\(superClass.interface.type.render(classInfo.goPackage))" when (!methodsToGenerate.isEmpty) { "" } @@ -209,6 +217,13 @@ local interface: String? = when (key != methodsToGenerate.keys.first) { "" } + let (field = if (field.type == classInfo.type) + (field) { + type = new Type.Pointer { + elem = field.type + } + } else field + ) "\tGet\(field.name)() \(field.type.render(classInfo.goPackage))" } "}" @@ -259,6 +274,13 @@ local function renderGetter(field: GoStructField): String = new Listing { when (field.docComment != null) { utils.renderDocComment(field.docComment!!, "") } + let (field = if (field.type == classInfo.type) + (field) { + type = new Type.Pointer { + elem = field.type + } + } else field + ) "func (rcv \(classInfo.struct.type.render(classInfo.goPackage))) Get\(field.name)() \(field.type.render(classInfo.goPackage)) {" "\treturn rcv.\(field.name)" "}" diff --git a/codegen/src/internal/GoMapping.pkl b/codegen/src/internal/GoMapping.pkl index 558bd1c..363d398 100644 --- a/codegen/src/internal/GoMapping.pkl +++ b/codegen/src/internal/GoMapping.pkl @@ -95,9 +95,9 @@ class Class extends GoMapping { || clazz.superclass.reflectee != Typed && clazz.superclass.reflectee != Module ) new Interface { - name = self.name + name = "I\(self.name)" type = new Type.Declared { - typeName = self.name + typeName = outer.name importPath = self.goPackage package = self.goPackageShort } @@ -107,18 +107,16 @@ class Class extends GoMapping { struct: Struct? = if (clazz.modifiers.contains("abstract")) null else - let (structName = if (interface == null) name else "\(name)Impl") + let (structName = name) new Struct { name = structName clazz = self.clazz - type = new Type.Pointer { - elem = new Type.Declared { + type = new Type.Declared { typeName = structName importPath = self.goPackage package = self.goPackageShort } } - } } diff --git a/codegen/src/internal/typegen.pkl b/codegen/src/internal/typegen.pkl index 8039960..d28a957 100644 --- a/codegen/src/internal/typegen.pkl +++ b/codegen/src/internal/typegen.pkl @@ -26,17 +26,17 @@ function generateType( enclosing: reflect.TypeDeclaration, seenMappings: List ): Type = - if (type is reflect.DeclaredType) + if (type is reflect.NullableType) + let (_elem = generateType(type.member, enclosing, seenMappings)) + // No double pointers + if (_elem is Type.Pointer) _elem + else new Type.Pointer { elem = _elem } + else if (type is reflect.DeclaredType) generateDeclaredType(type, enclosing, seenMappings) else if (type is reflect.ModuleType) let (moduleClass = enclosing.enclosingDeclaration.moduleClass) generateType(reflect.DeclaredType(moduleClass), moduleClass, seenMappings) else if (type is reflect.UnionType) generateUnionType(type, seenMappings) - else if (type is reflect.NullableType) - let (_elem = generateType(type.member, enclosing, seenMappings)) - // No double pointers - if (_elem is Type.Pointer) _elem - else new Type.Pointer { elem = _elem } else if (type is reflect.UnknownType) anyType else if (type is reflect.NothingType) throw("Unable to generate Go for the `nothing` type") else if (type is reflect.StringLiteralType) new Type.Declared { typeName = "string" } From d8a7bd9ad5e84c53d2826685bfd9c4fd42c9dcd4 Mon Sep 17 00:00:00 2001 From: Thomas Purchas Date: Sun, 30 Jun 2024 21:09:08 +0100 Subject: [PATCH 2/4] Update codegen snippets with new generated code We've made some big changes in how the codegen works, so we need to update all the snippet outputs to reflect that. --- .../snippet-tests/output/bugholder/A.pkl.go | 2 +- .../snippet-tests/output/bugholder/B.pkl.go | 12 +++++----- .../output/bugholder/Being.pkl.go | 2 +- .../output/bugholder/Bike.pkl.go | 2 +- .../snippet-tests/output/bugholder/Bug.pkl.go | 2 +- .../output/bugholder/BugHolder.pkl.go | 18 +++++++-------- .../snippet-tests/output/bugholder/C.pkl.go | 12 +++++----- .../snippet-tests/output/bugholder/D.pkl.go | 12 +++++----- .../output/bugholder/Person.pkl.go | 22 +++++++++---------- .../output/bugholder/ThisPerson.pkl.go | 22 +++++++++---------- .../output/bugholder/init.pkl.go | 10 ++++----- .../output/cyclicmodule/CyclicModule.pkl.go | 12 +++++----- .../emptyopenmodule/EmptyOpenModule.pkl.go | 14 ++++++------ .../output/emptyopenmodule/init.pkl.go | 2 +- .../explicitname/ExplicitlyCoolName.pkl.go | 12 +++++----- .../output/extendabstractclass/A.pkl.go | 2 +- .../output/extendabstractclass/B.pkl.go | 12 +++++----- .../ExtendsAbstractClass.pkl.go | 12 +++++----- .../output/extendabstractclass/init.pkl.go | 2 +- .../output/extendmodule/ExtendModule.pkl.go | 20 ++++++++--------- .../output/extendmodule/init.pkl.go | 2 +- .../extendopenclass/ExtendingOpenClass.pkl.go | 14 ++++++------ .../output/extendopenclass/MyClass.pkl.go | 12 +++++----- .../output/extendopenclass/MyClass2.pkl.go | 12 +++++----- .../output/extendopenclass/MyOpenClass.pkl.go | 8 +++---- .../output/extendopenclass/init.pkl.go | 6 ++--- .../hiddenproperties/HiddenProperties.pkl.go | 10 ++++----- .../output/moduletype/ModuleType.pkl.go | 12 +++++----- .../moduleusinglib/ModuleUsingLib.pkl.go | 12 +++++----- .../snippet-tests/output/override/Bar.pkl.go | 10 ++++----- .../snippet-tests/output/override/Foo.pkl.go | 2 +- .../output/override/Override.pkl.go | 12 +++++----- .../snippet-tests/output/override/init.pkl.go | 2 +- .../output/override2/MySubclass.pkl.go | 12 +++++----- .../output/override2/Override2.pkl.go | 16 +++++++------- .../output/override2/init.pkl.go | 4 ++-- .../output/structtags/StructTags.pkl.go | 10 ++++----- .../output/support/lib/Lib.pkl.go | 10 ++++----- .../output/support/lib/MyClass.pkl.go | 8 +++---- .../output/support/lib/init.pkl.go | 2 +- .../output/support/lib3/GoGoGo.pkl.go | 8 +++---- .../output/support/lib3/Lib3.pkl.go | 10 ++++----- .../output/support/lib3/init.pkl.go | 2 +- .../output/support/lib4/Lib4.pkl.go | 10 ++++----- .../output/support/lib4/MyLib4.pkl.go | 2 +- .../output/support/openmodule/MyModule.pkl.go | 16 +++++++------- .../output/support/openmodule/init.pkl.go | 2 +- .../snippet-tests/output/union/Union.pkl.go | 10 ++++----- .../unionnamekeyword/UnionNameKeyword.pkl.go | 10 ++++----- 49 files changed, 225 insertions(+), 225 deletions(-) diff --git a/codegen/snippet-tests/output/bugholder/A.pkl.go b/codegen/snippet-tests/output/bugholder/A.pkl.go index 8aea13c..652efec 100644 --- a/codegen/snippet-tests/output/bugholder/A.pkl.go +++ b/codegen/snippet-tests/output/bugholder/A.pkl.go @@ -1,6 +1,6 @@ // Code generated from Pkl module `org.foo.BugHolder`. DO NOT EDIT. package bugholder -type A interface { +type IA interface { GetA() string } diff --git a/codegen/snippet-tests/output/bugholder/B.pkl.go b/codegen/snippet-tests/output/bugholder/B.pkl.go index e6638b4..04e8447 100644 --- a/codegen/snippet-tests/output/bugholder/B.pkl.go +++ b/codegen/snippet-tests/output/bugholder/B.pkl.go @@ -1,24 +1,24 @@ // Code generated from Pkl module `org.foo.BugHolder`. DO NOT EDIT. package bugholder -type B interface { - A +type IB interface { + IA GetB() string } -var _ B = (*BImpl)(nil) +var _ IB = B{} -type BImpl struct { +type B struct { B string `pkl:"b"` A string `pkl:"a"` } -func (rcv *BImpl) GetB() string { +func (rcv B) GetB() string { return rcv.B } -func (rcv *BImpl) GetA() string { +func (rcv B) GetA() string { return rcv.A } diff --git a/codegen/snippet-tests/output/bugholder/Being.pkl.go b/codegen/snippet-tests/output/bugholder/Being.pkl.go index e852ab8..c7cf290 100644 --- a/codegen/snippet-tests/output/bugholder/Being.pkl.go +++ b/codegen/snippet-tests/output/bugholder/Being.pkl.go @@ -1,6 +1,6 @@ // Code generated from Pkl module `org.foo.BugHolder`. DO NOT EDIT. package bugholder -type Being interface { +type IBeing interface { GetIsAlive() bool } diff --git a/codegen/snippet-tests/output/bugholder/Bike.pkl.go b/codegen/snippet-tests/output/bugholder/Bike.pkl.go index 676d213..8a84815 100644 --- a/codegen/snippet-tests/output/bugholder/Bike.pkl.go +++ b/codegen/snippet-tests/output/bugholder/Bike.pkl.go @@ -7,5 +7,5 @@ type Bike struct { // Wheels are the front and back wheels. // // There are typically two of them. - Wheels []*Wheel `pkl:"wheels"` + Wheels []Wheel `pkl:"wheels"` } diff --git a/codegen/snippet-tests/output/bugholder/Bug.pkl.go b/codegen/snippet-tests/output/bugholder/Bug.pkl.go index ef6eae9..299bdd6 100644 --- a/codegen/snippet-tests/output/bugholder/Bug.pkl.go +++ b/codegen/snippet-tests/output/bugholder/Bug.pkl.go @@ -9,7 +9,7 @@ import ( type Bug struct { // The owner of this bug. - Owner *Person `pkl:"owner"` + Owner *IPerson `pkl:"owner"` // The age of this bug Age *int `pkl:"age"` diff --git a/codegen/snippet-tests/output/bugholder/BugHolder.pkl.go b/codegen/snippet-tests/output/bugholder/BugHolder.pkl.go index c975eb3..1ffb829 100644 --- a/codegen/snippet-tests/output/bugholder/BugHolder.pkl.go +++ b/codegen/snippet-tests/output/bugholder/BugHolder.pkl.go @@ -8,20 +8,20 @@ import ( ) type BugHolder struct { - Bug *Bug `pkl:"bug"` + Bug Bug `pkl:"bug"` - N蚊子 *Bug `pkl:"蚊子"` + N蚊子 Bug `pkl:"蚊子"` - ThisPerson ThisPerson `pkl:"thisPerson"` + ThisPerson IThisPerson `pkl:"thisPerson"` - D D `pkl:"d"` + D ID `pkl:"d"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a BugHolder -func LoadFromPath(ctx context.Context, path string) (ret *BugHolder, err error) { +func LoadFromPath(ctx context.Context, path string) (ret BugHolder, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return BugHolder{}, err } defer func() { cerr := evaluator.Close() @@ -34,10 +34,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *BugHolder, err error) } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a BugHolder -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*BugHolder, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (BugHolder, error) { var ret BugHolder if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return BugHolder{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/bugholder/C.pkl.go b/codegen/snippet-tests/output/bugholder/C.pkl.go index d9d3940..0f9a44e 100644 --- a/codegen/snippet-tests/output/bugholder/C.pkl.go +++ b/codegen/snippet-tests/output/bugholder/C.pkl.go @@ -1,20 +1,20 @@ // Code generated from Pkl module `org.foo.BugHolder`. DO NOT EDIT. package bugholder -type C interface { - B +type IC interface { + IB GetC() string } -var _ C = (*CImpl)(nil) +var _ IC = C{} -type CImpl struct { - *BImpl +type C struct { + B C string `pkl:"c"` } -func (rcv *CImpl) GetC() string { +func (rcv C) GetC() string { return rcv.C } diff --git a/codegen/snippet-tests/output/bugholder/D.pkl.go b/codegen/snippet-tests/output/bugholder/D.pkl.go index 3f57020..3fce452 100644 --- a/codegen/snippet-tests/output/bugholder/D.pkl.go +++ b/codegen/snippet-tests/output/bugholder/D.pkl.go @@ -1,20 +1,20 @@ // Code generated from Pkl module `org.foo.BugHolder`. DO NOT EDIT. package bugholder -type D interface { - C +type ID interface { + IC GetD() string } -var _ D = (*DImpl)(nil) +var _ ID = D{} -type DImpl struct { - *CImpl +type D struct { + C D string `pkl:"d"` } -func (rcv *DImpl) GetD() string { +func (rcv D) GetD() string { return rcv.D } diff --git a/codegen/snippet-tests/output/bugholder/Person.pkl.go b/codegen/snippet-tests/output/bugholder/Person.pkl.go index b7fe930..35e5e7b 100644 --- a/codegen/snippet-tests/output/bugholder/Person.pkl.go +++ b/codegen/snippet-tests/output/bugholder/Person.pkl.go @@ -1,10 +1,10 @@ // Code generated from Pkl module `org.foo.BugHolder`. DO NOT EDIT. package bugholder -type Person interface { - Being +type IPerson interface { + IBeing - GetBike() *Bike + GetBike() Bike GetFirstName() *uint16 @@ -13,13 +13,13 @@ type Person interface { GetThings() map[int]struct{} } -var _ Person = (*PersonImpl)(nil) +var _ IPerson = Person{} // A Person! -type PersonImpl struct { +type Person struct { IsAlive bool `pkl:"isAlive"` - Bike *Bike `pkl:"bike"` + Bike Bike `pkl:"bike"` // The person's first name FirstName *uint16 `pkl:"firstName"` @@ -30,24 +30,24 @@ type PersonImpl struct { Things map[int]struct{} `pkl:"things"` } -func (rcv *PersonImpl) GetIsAlive() bool { +func (rcv Person) GetIsAlive() bool { return rcv.IsAlive } -func (rcv *PersonImpl) GetBike() *Bike { +func (rcv Person) GetBike() Bike { return rcv.Bike } // The person's first name -func (rcv *PersonImpl) GetFirstName() *uint16 { +func (rcv Person) GetFirstName() *uint16 { return rcv.FirstName } // The person's last name -func (rcv *PersonImpl) GetLastName() map[string]*uint32 { +func (rcv Person) GetLastName() map[string]*uint32 { return rcv.LastName } -func (rcv *PersonImpl) GetThings() map[int]struct{} { +func (rcv Person) GetThings() map[int]struct{} { return rcv.Things } diff --git a/codegen/snippet-tests/output/bugholder/ThisPerson.pkl.go b/codegen/snippet-tests/output/bugholder/ThisPerson.pkl.go index ff66a6a..3819b09 100644 --- a/codegen/snippet-tests/output/bugholder/ThisPerson.pkl.go +++ b/codegen/snippet-tests/output/bugholder/ThisPerson.pkl.go @@ -1,28 +1,28 @@ // Code generated from Pkl module `org.foo.BugHolder`. DO NOT EDIT. package bugholder -type ThisPerson interface { - Person +type IThisPerson interface { + IPerson - GetMyself() ThisPerson + GetMyself() *IThisPerson - GetSomeoneElse() Person + GetSomeoneElse() IPerson } -var _ ThisPerson = (*ThisPersonImpl)(nil) +var _ IThisPerson = ThisPerson{} -type ThisPersonImpl struct { - *PersonImpl +type ThisPerson struct { + Person - Myself ThisPerson `pkl:"myself"` + Myself *IThisPerson `pkl:"myself"` - SomeoneElse Person `pkl:"someoneElse"` + SomeoneElse IPerson `pkl:"someoneElse"` } -func (rcv *ThisPersonImpl) GetMyself() ThisPerson { +func (rcv ThisPerson) GetMyself() *IThisPerson { return rcv.Myself } -func (rcv *ThisPersonImpl) GetSomeoneElse() Person { +func (rcv ThisPerson) GetSomeoneElse() IPerson { return rcv.SomeoneElse } diff --git a/codegen/snippet-tests/output/bugholder/init.pkl.go b/codegen/snippet-tests/output/bugholder/init.pkl.go index 6194911..10b77f4 100644 --- a/codegen/snippet-tests/output/bugholder/init.pkl.go +++ b/codegen/snippet-tests/output/bugholder/init.pkl.go @@ -6,11 +6,11 @@ import "github.com/apple/pkl-go/pkl" func init() { pkl.RegisterMapping("org.foo.BugHolder", BugHolder{}) pkl.RegisterMapping("org.foo.BugHolder#Bug", Bug{}) - pkl.RegisterMapping("org.foo.BugHolder#Person", PersonImpl{}) + pkl.RegisterMapping("org.foo.BugHolder#Person", Person{}) pkl.RegisterMapping("org.foo.BugHolder#Bike", Bike{}) pkl.RegisterMapping("org.foo.BugHolder#Wheel", Wheel{}) - pkl.RegisterMapping("org.foo.BugHolder#ThisPerson", ThisPersonImpl{}) - pkl.RegisterMapping("org.foo.BugHolder#D", DImpl{}) - pkl.RegisterMapping("org.foo.BugHolder#C", CImpl{}) - pkl.RegisterMapping("org.foo.BugHolder#B", BImpl{}) + pkl.RegisterMapping("org.foo.BugHolder#ThisPerson", ThisPerson{}) + pkl.RegisterMapping("org.foo.BugHolder#D", D{}) + pkl.RegisterMapping("org.foo.BugHolder#C", C{}) + pkl.RegisterMapping("org.foo.BugHolder#B", B{}) } diff --git a/codegen/snippet-tests/output/cyclicmodule/CyclicModule.pkl.go b/codegen/snippet-tests/output/cyclicmodule/CyclicModule.pkl.go index 0c186c6..0f4687e 100644 --- a/codegen/snippet-tests/output/cyclicmodule/CyclicModule.pkl.go +++ b/codegen/snippet-tests/output/cyclicmodule/CyclicModule.pkl.go @@ -8,14 +8,14 @@ import ( ) type CyclicModule struct { - Thing *Cyclic `pkl:"thing"` + Thing Cyclic `pkl:"thing"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a CyclicModule -func LoadFromPath(ctx context.Context, path string) (ret *CyclicModule, err error) { +func LoadFromPath(ctx context.Context, path string) (ret CyclicModule, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return CyclicModule{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *CyclicModule, err erro } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a CyclicModule -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*CyclicModule, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (CyclicModule, error) { var ret CyclicModule if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return CyclicModule{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/emptyopenmodule/EmptyOpenModule.pkl.go b/codegen/snippet-tests/output/emptyopenmodule/EmptyOpenModule.pkl.go index ed2c8d1..5609dd0 100644 --- a/codegen/snippet-tests/output/emptyopenmodule/EmptyOpenModule.pkl.go +++ b/codegen/snippet-tests/output/emptyopenmodule/EmptyOpenModule.pkl.go @@ -7,19 +7,19 @@ import ( "github.com/apple/pkl-go/pkl" ) -type EmptyOpenModule interface { +type IEmptyOpenModule interface { } -var _ EmptyOpenModule = (*EmptyOpenModuleImpl)(nil) +var _ IEmptyOpenModule = EmptyOpenModule{} -type EmptyOpenModuleImpl struct { +type EmptyOpenModule struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a EmptyOpenModule func LoadFromPath(ctx context.Context, path string) (ret EmptyOpenModule, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return EmptyOpenModule{}, err } defer func() { cerr := evaluator.Close() @@ -33,9 +33,9 @@ func LoadFromPath(ctx context.Context, path string) (ret EmptyOpenModule, err er // Load loads the pkl module at the given source and evaluates it with the given evaluator into a EmptyOpenModule func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (EmptyOpenModule, error) { - var ret EmptyOpenModuleImpl + var ret EmptyOpenModule if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return EmptyOpenModule{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/emptyopenmodule/init.pkl.go b/codegen/snippet-tests/output/emptyopenmodule/init.pkl.go index e93cc11..97065cb 100644 --- a/codegen/snippet-tests/output/emptyopenmodule/init.pkl.go +++ b/codegen/snippet-tests/output/emptyopenmodule/init.pkl.go @@ -4,5 +4,5 @@ package emptyopenmodule import "github.com/apple/pkl-go/pkl" func init() { - pkl.RegisterMapping("EmptyOpenModule", EmptyOpenModuleImpl{}) + pkl.RegisterMapping("EmptyOpenModule", EmptyOpenModule{}) } diff --git a/codegen/snippet-tests/output/explicitname/ExplicitlyCoolName.pkl.go b/codegen/snippet-tests/output/explicitname/ExplicitlyCoolName.pkl.go index 414aef2..e11d022 100644 --- a/codegen/snippet-tests/output/explicitname/ExplicitlyCoolName.pkl.go +++ b/codegen/snippet-tests/output/explicitname/ExplicitlyCoolName.pkl.go @@ -8,14 +8,14 @@ import ( ) type ExplicitlyCoolName struct { - MyCoolProp *SomethingVeryFunny `pkl:"myProp"` + MyCoolProp SomethingVeryFunny `pkl:"myProp"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a ExplicitlyCoolName -func LoadFromPath(ctx context.Context, path string) (ret *ExplicitlyCoolName, err error) { +func LoadFromPath(ctx context.Context, path string) (ret ExplicitlyCoolName, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return ExplicitlyCoolName{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *ExplicitlyCoolName, er } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a ExplicitlyCoolName -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*ExplicitlyCoolName, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (ExplicitlyCoolName, error) { var ret ExplicitlyCoolName if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return ExplicitlyCoolName{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/extendabstractclass/A.pkl.go b/codegen/snippet-tests/output/extendabstractclass/A.pkl.go index c4e67fd..cc5c834 100644 --- a/codegen/snippet-tests/output/extendabstractclass/A.pkl.go +++ b/codegen/snippet-tests/output/extendabstractclass/A.pkl.go @@ -1,6 +1,6 @@ // Code generated from Pkl module `ExtendsAbstractClass`. DO NOT EDIT. package extendabstractclass -type A interface { +type IA interface { GetB() string } diff --git a/codegen/snippet-tests/output/extendabstractclass/B.pkl.go b/codegen/snippet-tests/output/extendabstractclass/B.pkl.go index c4eeb11..4b9704a 100644 --- a/codegen/snippet-tests/output/extendabstractclass/B.pkl.go +++ b/codegen/snippet-tests/output/extendabstractclass/B.pkl.go @@ -1,24 +1,24 @@ // Code generated from Pkl module `ExtendsAbstractClass`. DO NOT EDIT. package extendabstractclass -type B interface { - A +type IB interface { + IA GetC() string } -var _ B = (*BImpl)(nil) +var _ IB = B{} -type BImpl struct { +type B struct { B string `pkl:"b"` C string `pkl:"c"` } -func (rcv *BImpl) GetB() string { +func (rcv B) GetB() string { return rcv.B } -func (rcv *BImpl) GetC() string { +func (rcv B) GetC() string { return rcv.C } diff --git a/codegen/snippet-tests/output/extendabstractclass/ExtendsAbstractClass.pkl.go b/codegen/snippet-tests/output/extendabstractclass/ExtendsAbstractClass.pkl.go index 8599679..fe92fce 100644 --- a/codegen/snippet-tests/output/extendabstractclass/ExtendsAbstractClass.pkl.go +++ b/codegen/snippet-tests/output/extendabstractclass/ExtendsAbstractClass.pkl.go @@ -8,14 +8,14 @@ import ( ) type ExtendsAbstractClass struct { - A A `pkl:"a"` + A IA `pkl:"a"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a ExtendsAbstractClass -func LoadFromPath(ctx context.Context, path string) (ret *ExtendsAbstractClass, err error) { +func LoadFromPath(ctx context.Context, path string) (ret ExtendsAbstractClass, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return ExtendsAbstractClass{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *ExtendsAbstractClass, } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a ExtendsAbstractClass -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*ExtendsAbstractClass, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (ExtendsAbstractClass, error) { var ret ExtendsAbstractClass if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return ExtendsAbstractClass{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/extendabstractclass/init.pkl.go b/codegen/snippet-tests/output/extendabstractclass/init.pkl.go index b5dd46d..e9be50b 100644 --- a/codegen/snippet-tests/output/extendabstractclass/init.pkl.go +++ b/codegen/snippet-tests/output/extendabstractclass/init.pkl.go @@ -5,5 +5,5 @@ import "github.com/apple/pkl-go/pkl" func init() { pkl.RegisterMapping("ExtendsAbstractClass", ExtendsAbstractClass{}) - pkl.RegisterMapping("ExtendsAbstractClass#B", BImpl{}) + pkl.RegisterMapping("ExtendsAbstractClass#B", B{}) } diff --git a/codegen/snippet-tests/output/extendmodule/ExtendModule.pkl.go b/codegen/snippet-tests/output/extendmodule/ExtendModule.pkl.go index 936de62..1a485cd 100644 --- a/codegen/snippet-tests/output/extendmodule/ExtendModule.pkl.go +++ b/codegen/snippet-tests/output/extendmodule/ExtendModule.pkl.go @@ -8,21 +8,21 @@ import ( "github.com/apple/pkl-go/pkl" ) -type ExtendModule interface { - openmodule.MyModule +type IExtendModule interface { + openmodule.IMyModule GetBar() string } -var _ ExtendModule = (*ExtendModuleImpl)(nil) +var _ IExtendModule = ExtendModule{} -type ExtendModuleImpl struct { - *openmodule.MyModuleImpl +type ExtendModule struct { + openmodule.MyModule Bar string `pkl:"bar"` } -func (rcv *ExtendModuleImpl) GetBar() string { +func (rcv ExtendModule) GetBar() string { return rcv.Bar } @@ -30,7 +30,7 @@ func (rcv *ExtendModuleImpl) GetBar() string { func LoadFromPath(ctx context.Context, path string) (ret ExtendModule, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return ExtendModule{}, err } defer func() { cerr := evaluator.Close() @@ -44,9 +44,9 @@ func LoadFromPath(ctx context.Context, path string) (ret ExtendModule, err error // Load loads the pkl module at the given source and evaluates it with the given evaluator into a ExtendModule func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (ExtendModule, error) { - var ret ExtendModuleImpl + var ret ExtendModule if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return ExtendModule{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/extendmodule/init.pkl.go b/codegen/snippet-tests/output/extendmodule/init.pkl.go index ec9f22a..9b3fa5b 100644 --- a/codegen/snippet-tests/output/extendmodule/init.pkl.go +++ b/codegen/snippet-tests/output/extendmodule/init.pkl.go @@ -4,5 +4,5 @@ package extendmodule import "github.com/apple/pkl-go/pkl" func init() { - pkl.RegisterMapping("ExtendModule", ExtendModuleImpl{}) + pkl.RegisterMapping("ExtendModule", ExtendModule{}) } diff --git a/codegen/snippet-tests/output/extendopenclass/ExtendingOpenClass.pkl.go b/codegen/snippet-tests/output/extendopenclass/ExtendingOpenClass.pkl.go index b03cf7b..c63b8dc 100644 --- a/codegen/snippet-tests/output/extendopenclass/ExtendingOpenClass.pkl.go +++ b/codegen/snippet-tests/output/extendopenclass/ExtendingOpenClass.pkl.go @@ -8,16 +8,16 @@ import ( ) type ExtendingOpenClass struct { - Res1 MyClass `pkl:"res1"` + Res1 IMyClass `pkl:"res1"` - Res2 MyClass2 `pkl:"res2"` + Res2 IMyClass2 `pkl:"res2"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a ExtendingOpenClass -func LoadFromPath(ctx context.Context, path string) (ret *ExtendingOpenClass, err error) { +func LoadFromPath(ctx context.Context, path string) (ret ExtendingOpenClass, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return ExtendingOpenClass{}, err } defer func() { cerr := evaluator.Close() @@ -30,10 +30,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *ExtendingOpenClass, er } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a ExtendingOpenClass -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*ExtendingOpenClass, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (ExtendingOpenClass, error) { var ret ExtendingOpenClass if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return ExtendingOpenClass{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/extendopenclass/MyClass.pkl.go b/codegen/snippet-tests/output/extendopenclass/MyClass.pkl.go index ab5a4b1..2d396ad 100644 --- a/codegen/snippet-tests/output/extendopenclass/MyClass.pkl.go +++ b/codegen/snippet-tests/output/extendopenclass/MyClass.pkl.go @@ -1,20 +1,20 @@ // Code generated from Pkl module `ExtendingOpenClass`. DO NOT EDIT. package extendopenclass -type MyClass interface { - MyOpenClass +type IMyClass interface { + IMyOpenClass GetMyBoolean() bool } -var _ MyClass = (*MyClassImpl)(nil) +var _ IMyClass = MyClass{} -type MyClassImpl struct { - *MyOpenClassImpl +type MyClass struct { + MyOpenClass MyBoolean bool `pkl:"myBoolean"` } -func (rcv *MyClassImpl) GetMyBoolean() bool { +func (rcv MyClass) GetMyBoolean() bool { return rcv.MyBoolean } diff --git a/codegen/snippet-tests/output/extendopenclass/MyClass2.pkl.go b/codegen/snippet-tests/output/extendopenclass/MyClass2.pkl.go index 75f0f83..23b1eb1 100644 --- a/codegen/snippet-tests/output/extendopenclass/MyClass2.pkl.go +++ b/codegen/snippet-tests/output/extendopenclass/MyClass2.pkl.go @@ -3,20 +3,20 @@ package extendopenclass import "github.com/apple/pkl-go/codegen/snippet-tests/output/support/lib3" -type MyClass2 interface { - lib3.GoGoGo +type IMyClass2 interface { + lib3.IGoGoGo GetMyBoolean() bool } -var _ MyClass2 = (*MyClass2Impl)(nil) +var _ IMyClass2 = MyClass2{} -type MyClass2Impl struct { - *lib3.GoGoGoImpl +type MyClass2 struct { + lib3.GoGoGo MyBoolean bool `pkl:"myBoolean"` } -func (rcv *MyClass2Impl) GetMyBoolean() bool { +func (rcv MyClass2) GetMyBoolean() bool { return rcv.MyBoolean } diff --git a/codegen/snippet-tests/output/extendopenclass/MyOpenClass.pkl.go b/codegen/snippet-tests/output/extendopenclass/MyOpenClass.pkl.go index aa1f64f..1c7da19 100644 --- a/codegen/snippet-tests/output/extendopenclass/MyOpenClass.pkl.go +++ b/codegen/snippet-tests/output/extendopenclass/MyOpenClass.pkl.go @@ -1,16 +1,16 @@ // Code generated from Pkl module `ExtendingOpenClass`. DO NOT EDIT. package extendopenclass -type MyOpenClass interface { +type IMyOpenClass interface { GetMyStr() string } -var _ MyOpenClass = (*MyOpenClassImpl)(nil) +var _ IMyOpenClass = MyOpenClass{} -type MyOpenClassImpl struct { +type MyOpenClass struct { MyStr string `pkl:"myStr"` } -func (rcv *MyOpenClassImpl) GetMyStr() string { +func (rcv MyOpenClass) GetMyStr() string { return rcv.MyStr } diff --git a/codegen/snippet-tests/output/extendopenclass/init.pkl.go b/codegen/snippet-tests/output/extendopenclass/init.pkl.go index 9a8e3a6..5bba1d1 100644 --- a/codegen/snippet-tests/output/extendopenclass/init.pkl.go +++ b/codegen/snippet-tests/output/extendopenclass/init.pkl.go @@ -5,7 +5,7 @@ import "github.com/apple/pkl-go/pkl" func init() { pkl.RegisterMapping("ExtendingOpenClass", ExtendingOpenClass{}) - pkl.RegisterMapping("ExtendingOpenClass#MyClass", MyClassImpl{}) - pkl.RegisterMapping("ExtendingOpenClass#MyOpenClass", MyOpenClassImpl{}) - pkl.RegisterMapping("ExtendingOpenClass#MyClass2", MyClass2Impl{}) + pkl.RegisterMapping("ExtendingOpenClass#MyClass", MyClass{}) + pkl.RegisterMapping("ExtendingOpenClass#MyOpenClass", MyOpenClass{}) + pkl.RegisterMapping("ExtendingOpenClass#MyClass2", MyClass2{}) } diff --git a/codegen/snippet-tests/output/hiddenproperties/HiddenProperties.pkl.go b/codegen/snippet-tests/output/hiddenproperties/HiddenProperties.pkl.go index 8ae108b..3d87860 100644 --- a/codegen/snippet-tests/output/hiddenproperties/HiddenProperties.pkl.go +++ b/codegen/snippet-tests/output/hiddenproperties/HiddenProperties.pkl.go @@ -12,10 +12,10 @@ type HiddenProperties struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a HiddenProperties -func LoadFromPath(ctx context.Context, path string) (ret *HiddenProperties, err error) { +func LoadFromPath(ctx context.Context, path string) (ret HiddenProperties, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return HiddenProperties{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *HiddenProperties, err } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a HiddenProperties -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*HiddenProperties, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (HiddenProperties, error) { var ret HiddenProperties if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return HiddenProperties{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/moduletype/ModuleType.pkl.go b/codegen/snippet-tests/output/moduletype/ModuleType.pkl.go index 2f60f2b..cec6039 100644 --- a/codegen/snippet-tests/output/moduletype/ModuleType.pkl.go +++ b/codegen/snippet-tests/output/moduletype/ModuleType.pkl.go @@ -13,16 +13,16 @@ type ModuleType struct { Foo *ModuleType `pkl:"foo"` - Lib *lib4.MyLib4 `pkl:"lib"` + Lib lib4.MyLib4 `pkl:"lib"` FooAgain *ModuleType `pkl:"fooAgain"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a ModuleType -func LoadFromPath(ctx context.Context, path string) (ret *ModuleType, err error) { +func LoadFromPath(ctx context.Context, path string) (ret ModuleType, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return ModuleType{}, err } defer func() { cerr := evaluator.Close() @@ -35,10 +35,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *ModuleType, err error) } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a ModuleType -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*ModuleType, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (ModuleType, error) { var ret ModuleType if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return ModuleType{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/moduleusinglib/ModuleUsingLib.pkl.go b/codegen/snippet-tests/output/moduleusinglib/ModuleUsingLib.pkl.go index e024d9d..09d05a8 100644 --- a/codegen/snippet-tests/output/moduleusinglib/ModuleUsingLib.pkl.go +++ b/codegen/snippet-tests/output/moduleusinglib/ModuleUsingLib.pkl.go @@ -11,7 +11,7 @@ import ( ) type ModuleUsingLib struct { - Res []lib.MyClass `pkl:"res"` + Res []lib.IMyClass `pkl:"res"` Res2 myenum.MyEnum `pkl:"res2"` @@ -21,10 +21,10 @@ type ModuleUsingLib struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a ModuleUsingLib -func LoadFromPath(ctx context.Context, path string) (ret *ModuleUsingLib, err error) { +func LoadFromPath(ctx context.Context, path string) (ret ModuleUsingLib, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return ModuleUsingLib{}, err } defer func() { cerr := evaluator.Close() @@ -37,10 +37,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *ModuleUsingLib, err er } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a ModuleUsingLib -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*ModuleUsingLib, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (ModuleUsingLib, error) { var ret ModuleUsingLib if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return ModuleUsingLib{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/override/Bar.pkl.go b/codegen/snippet-tests/output/override/Bar.pkl.go index 8ae0e66..15cb1d6 100644 --- a/codegen/snippet-tests/output/override/Bar.pkl.go +++ b/codegen/snippet-tests/output/override/Bar.pkl.go @@ -1,16 +1,16 @@ // Code generated from Pkl module `override`. DO NOT EDIT. package override -type Bar interface { - Foo +type IBar interface { + IFoo } -var _ Bar = (*BarImpl)(nil) +var _ IBar = Bar{} -type BarImpl struct { +type Bar struct { MyProp string `pkl:"myProp"` } -func (rcv *BarImpl) GetMyProp() string { +func (rcv Bar) GetMyProp() string { return rcv.MyProp } diff --git a/codegen/snippet-tests/output/override/Foo.pkl.go b/codegen/snippet-tests/output/override/Foo.pkl.go index 657b5ea..2f84652 100644 --- a/codegen/snippet-tests/output/override/Foo.pkl.go +++ b/codegen/snippet-tests/output/override/Foo.pkl.go @@ -1,6 +1,6 @@ // Code generated from Pkl module `override`. DO NOT EDIT. package override -type Foo interface { +type IFoo interface { GetMyProp() string } diff --git a/codegen/snippet-tests/output/override/Override.pkl.go b/codegen/snippet-tests/output/override/Override.pkl.go index b447298..ad5c7a5 100644 --- a/codegen/snippet-tests/output/override/Override.pkl.go +++ b/codegen/snippet-tests/output/override/Override.pkl.go @@ -8,14 +8,14 @@ import ( ) type Override struct { - Foo Foo `pkl:"foo"` + Foo IFoo `pkl:"foo"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a Override -func LoadFromPath(ctx context.Context, path string) (ret *Override, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Override, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Override{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Override, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Override -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Override, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Override, error) { var ret Override if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Override{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/override/init.pkl.go b/codegen/snippet-tests/output/override/init.pkl.go index f51b9b5..0a7923c 100644 --- a/codegen/snippet-tests/output/override/init.pkl.go +++ b/codegen/snippet-tests/output/override/init.pkl.go @@ -5,5 +5,5 @@ import "github.com/apple/pkl-go/pkl" func init() { pkl.RegisterMapping("override", Override{}) - pkl.RegisterMapping("override#Bar", BarImpl{}) + pkl.RegisterMapping("override#Bar", Bar{}) } diff --git a/codegen/snippet-tests/output/override2/MySubclass.pkl.go b/codegen/snippet-tests/output/override2/MySubclass.pkl.go index 0724854..188f932 100644 --- a/codegen/snippet-tests/output/override2/MySubclass.pkl.go +++ b/codegen/snippet-tests/output/override2/MySubclass.pkl.go @@ -1,22 +1,22 @@ // Code generated from Pkl module `Override2`. DO NOT EDIT. package override2 -type MySubclass interface { - Override2 +type IMySubclass interface { + IOverride2 GetFoo() string } -var _ MySubclass = (*MySubclassImpl)(nil) +var _ IMySubclass = MySubclass{} -type MySubclassImpl struct { - *Override2Impl +type MySubclass struct { + Override2 // Different doc comments Foo string `pkl:"foo"` } // Different doc comments -func (rcv *MySubclassImpl) GetFoo() string { +func (rcv MySubclass) GetFoo() string { return rcv.Foo } diff --git a/codegen/snippet-tests/output/override2/Override2.pkl.go b/codegen/snippet-tests/output/override2/Override2.pkl.go index d35fd89..cc3bb3b 100644 --- a/codegen/snippet-tests/output/override2/Override2.pkl.go +++ b/codegen/snippet-tests/output/override2/Override2.pkl.go @@ -7,19 +7,19 @@ import ( "github.com/apple/pkl-go/pkl" ) -type Override2 interface { +type IOverride2 interface { GetFoo() string } -var _ Override2 = (*Override2Impl)(nil) +var _ IOverride2 = Override2{} -type Override2Impl struct { +type Override2 struct { // Doc comments Foo string `pkl:"foo"` } // Doc comments -func (rcv *Override2Impl) GetFoo() string { +func (rcv Override2) GetFoo() string { return rcv.Foo } @@ -27,7 +27,7 @@ func (rcv *Override2Impl) GetFoo() string { func LoadFromPath(ctx context.Context, path string) (ret Override2, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Override2{}, err } defer func() { cerr := evaluator.Close() @@ -41,9 +41,9 @@ func LoadFromPath(ctx context.Context, path string) (ret Override2, err error) { // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Override2 func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Override2, error) { - var ret Override2Impl + var ret Override2 if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Override2{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/override2/init.pkl.go b/codegen/snippet-tests/output/override2/init.pkl.go index 921a38e..f82379b 100644 --- a/codegen/snippet-tests/output/override2/init.pkl.go +++ b/codegen/snippet-tests/output/override2/init.pkl.go @@ -4,6 +4,6 @@ package override2 import "github.com/apple/pkl-go/pkl" func init() { - pkl.RegisterMapping("Override2", Override2Impl{}) - pkl.RegisterMapping("Override2#MySubclass", MySubclassImpl{}) + pkl.RegisterMapping("Override2", Override2{}) + pkl.RegisterMapping("Override2#MySubclass", MySubclass{}) } diff --git a/codegen/snippet-tests/output/structtags/StructTags.pkl.go b/codegen/snippet-tests/output/structtags/StructTags.pkl.go index d2f1004..653323c 100644 --- a/codegen/snippet-tests/output/structtags/StructTags.pkl.go +++ b/codegen/snippet-tests/output/structtags/StructTags.pkl.go @@ -32,10 +32,10 @@ type StructTags struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a StructTags -func LoadFromPath(ctx context.Context, path string) (ret *StructTags, err error) { +func LoadFromPath(ctx context.Context, path string) (ret StructTags, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return StructTags{}, err } defer func() { cerr := evaluator.Close() @@ -48,10 +48,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *StructTags, err error) } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a StructTags -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*StructTags, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (StructTags, error) { var ret StructTags if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return StructTags{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/support/lib/Lib.pkl.go b/codegen/snippet-tests/output/support/lib/Lib.pkl.go index 3155a02..94237d7 100644 --- a/codegen/snippet-tests/output/support/lib/Lib.pkl.go +++ b/codegen/snippet-tests/output/support/lib/Lib.pkl.go @@ -11,10 +11,10 @@ type Lib struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Lib -func LoadFromPath(ctx context.Context, path string) (ret *Lib, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Lib, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Lib{}, err } defer func() { cerr := evaluator.Close() @@ -27,10 +27,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Lib, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Lib -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Lib, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Lib, error) { var ret Lib if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Lib{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/support/lib/MyClass.pkl.go b/codegen/snippet-tests/output/support/lib/MyClass.pkl.go index f1fe8c3..debaaaf 100644 --- a/codegen/snippet-tests/output/support/lib/MyClass.pkl.go +++ b/codegen/snippet-tests/output/support/lib/MyClass.pkl.go @@ -1,16 +1,16 @@ // Code generated from Pkl module `lib`. DO NOT EDIT. package lib -type MyClass interface { +type IMyClass interface { GetThing() string } -var _ MyClass = (*MyClassImpl)(nil) +var _ IMyClass = MyClass{} -type MyClassImpl struct { +type MyClass struct { Thing string `pkl:"thing"` } -func (rcv *MyClassImpl) GetThing() string { +func (rcv MyClass) GetThing() string { return rcv.Thing } diff --git a/codegen/snippet-tests/output/support/lib/init.pkl.go b/codegen/snippet-tests/output/support/lib/init.pkl.go index 599f09e..fec0737 100644 --- a/codegen/snippet-tests/output/support/lib/init.pkl.go +++ b/codegen/snippet-tests/output/support/lib/init.pkl.go @@ -4,6 +4,6 @@ package lib import "github.com/apple/pkl-go/pkl" func init() { - pkl.RegisterMapping("lib#MyClass", MyClassImpl{}) + pkl.RegisterMapping("lib#MyClass", MyClass{}) pkl.RegisterMapping("lib", Lib{}) } diff --git a/codegen/snippet-tests/output/support/lib3/GoGoGo.pkl.go b/codegen/snippet-tests/output/support/lib3/GoGoGo.pkl.go index d2e18b7..c4a3599 100644 --- a/codegen/snippet-tests/output/support/lib3/GoGoGo.pkl.go +++ b/codegen/snippet-tests/output/support/lib3/GoGoGo.pkl.go @@ -1,16 +1,16 @@ // Code generated from Pkl module `lib3`. DO NOT EDIT. package lib3 -type GoGoGo interface { +type IGoGoGo interface { GetDuck() string } -var _ GoGoGo = (*GoGoGoImpl)(nil) +var _ IGoGoGo = GoGoGo{} -type GoGoGoImpl struct { +type GoGoGo struct { Duck string `pkl:"duck"` } -func (rcv *GoGoGoImpl) GetDuck() string { +func (rcv GoGoGo) GetDuck() string { return rcv.Duck } diff --git a/codegen/snippet-tests/output/support/lib3/Lib3.pkl.go b/codegen/snippet-tests/output/support/lib3/Lib3.pkl.go index 4d31e86..497e3b3 100644 --- a/codegen/snippet-tests/output/support/lib3/Lib3.pkl.go +++ b/codegen/snippet-tests/output/support/lib3/Lib3.pkl.go @@ -11,10 +11,10 @@ type Lib3 struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Lib3 -func LoadFromPath(ctx context.Context, path string) (ret *Lib3, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Lib3, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Lib3{}, err } defer func() { cerr := evaluator.Close() @@ -27,10 +27,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Lib3, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Lib3 -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Lib3, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Lib3, error) { var ret Lib3 if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Lib3{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/support/lib3/init.pkl.go b/codegen/snippet-tests/output/support/lib3/init.pkl.go index c0a6088..90a63c7 100644 --- a/codegen/snippet-tests/output/support/lib3/init.pkl.go +++ b/codegen/snippet-tests/output/support/lib3/init.pkl.go @@ -4,6 +4,6 @@ package lib3 import "github.com/apple/pkl-go/pkl" func init() { - pkl.RegisterMapping("lib3#GoGoGo", GoGoGoImpl{}) + pkl.RegisterMapping("lib3#GoGoGo", GoGoGo{}) pkl.RegisterMapping("lib3", Lib3{}) } diff --git a/codegen/snippet-tests/output/support/lib4/Lib4.pkl.go b/codegen/snippet-tests/output/support/lib4/Lib4.pkl.go index 7626de8..c7b2ca9 100644 --- a/codegen/snippet-tests/output/support/lib4/Lib4.pkl.go +++ b/codegen/snippet-tests/output/support/lib4/Lib4.pkl.go @@ -12,10 +12,10 @@ type Lib4 struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Lib4 -func LoadFromPath(ctx context.Context, path string) (ret *Lib4, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Lib4, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Lib4{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Lib4, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Lib4 -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Lib4, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Lib4, error) { var ret Lib4 if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Lib4{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/support/lib4/MyLib4.pkl.go b/codegen/snippet-tests/output/support/lib4/MyLib4.pkl.go index ba1ee23..360454e 100644 --- a/codegen/snippet-tests/output/support/lib4/MyLib4.pkl.go +++ b/codegen/snippet-tests/output/support/lib4/MyLib4.pkl.go @@ -2,5 +2,5 @@ package lib4 type MyLib4 struct { - Foo *Lib4 `pkl:"foo"` + Foo Lib4 `pkl:"foo"` } diff --git a/codegen/snippet-tests/output/support/openmodule/MyModule.pkl.go b/codegen/snippet-tests/output/support/openmodule/MyModule.pkl.go index 819cd6f..323316b 100644 --- a/codegen/snippet-tests/output/support/openmodule/MyModule.pkl.go +++ b/codegen/snippet-tests/output/support/openmodule/MyModule.pkl.go @@ -7,17 +7,17 @@ import ( "github.com/apple/pkl-go/pkl" ) -type MyModule interface { +type IMyModule interface { GetFoo() string } -var _ MyModule = (*MyModuleImpl)(nil) +var _ IMyModule = MyModule{} -type MyModuleImpl struct { +type MyModule struct { Foo string `pkl:"foo"` } -func (rcv *MyModuleImpl) GetFoo() string { +func (rcv MyModule) GetFoo() string { return rcv.Foo } @@ -25,7 +25,7 @@ func (rcv *MyModuleImpl) GetFoo() string { func LoadFromPath(ctx context.Context, path string) (ret MyModule, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return MyModule{}, err } defer func() { cerr := evaluator.Close() @@ -39,9 +39,9 @@ func LoadFromPath(ctx context.Context, path string) (ret MyModule, err error) { // Load loads the pkl module at the given source and evaluates it with the given evaluator into a MyModule func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (MyModule, error) { - var ret MyModuleImpl + var ret MyModule if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return MyModule{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/support/openmodule/init.pkl.go b/codegen/snippet-tests/output/support/openmodule/init.pkl.go index a1e95c5..8d1af25 100644 --- a/codegen/snippet-tests/output/support/openmodule/init.pkl.go +++ b/codegen/snippet-tests/output/support/openmodule/init.pkl.go @@ -4,5 +4,5 @@ package openmodule import "github.com/apple/pkl-go/pkl" func init() { - pkl.RegisterMapping("MyModule", MyModuleImpl{}) + pkl.RegisterMapping("MyModule", MyModule{}) } diff --git a/codegen/snippet-tests/output/union/Union.pkl.go b/codegen/snippet-tests/output/union/Union.pkl.go index 6b26ed1..5f62f3e 100644 --- a/codegen/snippet-tests/output/union/Union.pkl.go +++ b/codegen/snippet-tests/output/union/Union.pkl.go @@ -26,10 +26,10 @@ type Union struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Union -func LoadFromPath(ctx context.Context, path string) (ret *Union, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Union, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Union{}, err } defer func() { cerr := evaluator.Close() @@ -42,10 +42,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Union, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Union -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Union, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Union, error) { var ret Union if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Union{}, err } - return &ret, nil + return ret, nil } diff --git a/codegen/snippet-tests/output/unionnamekeyword/UnionNameKeyword.pkl.go b/codegen/snippet-tests/output/unionnamekeyword/UnionNameKeyword.pkl.go index d7ee5e9..d8e6bbd 100644 --- a/codegen/snippet-tests/output/unionnamekeyword/UnionNameKeyword.pkl.go +++ b/codegen/snippet-tests/output/unionnamekeyword/UnionNameKeyword.pkl.go @@ -13,10 +13,10 @@ type UnionNameKeyword struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a UnionNameKeyword -func LoadFromPath(ctx context.Context, path string) (ret *UnionNameKeyword, err error) { +func LoadFromPath(ctx context.Context, path string) (ret UnionNameKeyword, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return UnionNameKeyword{}, err } defer func() { cerr := evaluator.Close() @@ -29,10 +29,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *UnionNameKeyword, err } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a UnionNameKeyword -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*UnionNameKeyword, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (UnionNameKeyword, error) { var ret UnionNameKeyword if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return UnionNameKeyword{}, err } - return &ret, nil + return ret, nil } From 66064d3cb1e7af3929ebcdba44893d18cc1966bd Mon Sep 17 00:00:00 2001 From: Thomas Purchas Date: Sun, 30 Jun 2024 20:44:03 +0100 Subject: [PATCH 3/4] Update the test fixtures using the new codegenerator 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. --- pkl/test_fixtures/gen/any/Any.pkl.go | 10 +++++----- pkl/test_fixtures/gen/classes/Animal.pkl.go | 2 +- pkl/test_fixtures/gen/classes/Cat.pkl.go | 12 ++++++------ pkl/test_fixtures/gen/classes/Classes.pkl.go | 16 ++++++++-------- pkl/test_fixtures/gen/classes/Dog.pkl.go | 14 +++++++------- pkl/test_fixtures/gen/classes/Greyhound.pkl.go | 12 ++++++------ pkl/test_fixtures/gen/classes/init.pkl.go | 6 +++--- .../gen/collections/Collections.pkl.go | 10 +++++----- pkl/test_fixtures/gen/datasize/Datasize.pkl.go | 10 +++++----- pkl/test_fixtures/gen/duration/Duration.pkl.go | 10 +++++----- pkl/test_fixtures/gen/dynamic/Dynamic.pkl.go | 10 +++++----- pkl/test_fixtures/gen/nullables/Nullables.pkl.go | 10 +++++----- .../gen/primitives/Primitives.pkl.go | 10 +++++----- pkl/test_fixtures/gen/unions/Unions.pkl.go | 10 +++++----- .../gen/unknown_type/UnknownType.pkl.go | 10 +++++----- 15 files changed, 76 insertions(+), 76 deletions(-) diff --git a/pkl/test_fixtures/gen/any/Any.pkl.go b/pkl/test_fixtures/gen/any/Any.pkl.go index 1bc3b6b..5bdccec 100644 --- a/pkl/test_fixtures/gen/any/Any.pkl.go +++ b/pkl/test_fixtures/gen/any/Any.pkl.go @@ -16,10 +16,10 @@ type Any struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Any -func LoadFromPath(ctx context.Context, path string) (ret *Any, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Any, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Any{}, err } defer func() { cerr := evaluator.Close() @@ -32,10 +32,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Any, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Any -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Any, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Any, error) { var ret Any if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Any{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/classes/Animal.pkl.go b/pkl/test_fixtures/gen/classes/Animal.pkl.go index b185795..140cecc 100644 --- a/pkl/test_fixtures/gen/classes/Animal.pkl.go +++ b/pkl/test_fixtures/gen/classes/Animal.pkl.go @@ -1,6 +1,6 @@ // Code generated from Pkl module `classes`. DO NOT EDIT. package classes -type Animal interface { +type IAnimal interface { GetName() string } diff --git a/pkl/test_fixtures/gen/classes/Cat.pkl.go b/pkl/test_fixtures/gen/classes/Cat.pkl.go index 84aba1a..2e97318 100644 --- a/pkl/test_fixtures/gen/classes/Cat.pkl.go +++ b/pkl/test_fixtures/gen/classes/Cat.pkl.go @@ -1,24 +1,24 @@ // Code generated from Pkl module `classes`. DO NOT EDIT. package classes -type Cat interface { - Animal +type ICat interface { + IAnimal GetMeows() bool } -var _ Cat = (*CatImpl)(nil) +var _ ICat = Cat{} -type CatImpl struct { +type Cat struct { Meows bool `pkl:"meows"` Name string `pkl:"name"` } -func (rcv *CatImpl) GetMeows() bool { +func (rcv Cat) GetMeows() bool { return rcv.Meows } -func (rcv *CatImpl) GetName() string { +func (rcv Cat) GetName() string { return rcv.Name } diff --git a/pkl/test_fixtures/gen/classes/Classes.pkl.go b/pkl/test_fixtures/gen/classes/Classes.pkl.go index 13f5089..3e0fc12 100644 --- a/pkl/test_fixtures/gen/classes/Classes.pkl.go +++ b/pkl/test_fixtures/gen/classes/Classes.pkl.go @@ -8,18 +8,18 @@ import ( ) type Classes struct { - Animals []Animal `pkl:"animals"` + Animals []IAnimal `pkl:"animals"` - MyAnimal Animal `pkl:"myAnimal"` + MyAnimal IAnimal `pkl:"myAnimal"` - House *House `pkl:"house"` + House House `pkl:"house"` } // LoadFromPath loads the pkl module at the given path and evaluates it into a Classes -func LoadFromPath(ctx context.Context, path string) (ret *Classes, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Classes, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Classes{}, err } defer func() { cerr := evaluator.Close() @@ -32,10 +32,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Classes, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Classes -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Classes, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Classes, error) { var ret Classes if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Classes{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/classes/Dog.pkl.go b/pkl/test_fixtures/gen/classes/Dog.pkl.go index 2f23bbc..62aee14 100644 --- a/pkl/test_fixtures/gen/classes/Dog.pkl.go +++ b/pkl/test_fixtures/gen/classes/Dog.pkl.go @@ -1,17 +1,17 @@ // Code generated from Pkl module `classes`. DO NOT EDIT. package classes -type Dog interface { - Animal +type IDog interface { + IAnimal GetBarks() bool GetBreed() string } -var _ Dog = (*DogImpl)(nil) +var _ IDog = Dog{} -type DogImpl struct { +type Dog struct { Barks bool `pkl:"barks"` Breed string `pkl:"breed"` @@ -19,14 +19,14 @@ type DogImpl struct { Name string `pkl:"name"` } -func (rcv *DogImpl) GetBarks() bool { +func (rcv Dog) GetBarks() bool { return rcv.Barks } -func (rcv *DogImpl) GetBreed() string { +func (rcv Dog) GetBreed() string { return rcv.Breed } -func (rcv *DogImpl) GetName() string { +func (rcv Dog) GetName() string { return rcv.Name } diff --git a/pkl/test_fixtures/gen/classes/Greyhound.pkl.go b/pkl/test_fixtures/gen/classes/Greyhound.pkl.go index b891025..28e6600 100644 --- a/pkl/test_fixtures/gen/classes/Greyhound.pkl.go +++ b/pkl/test_fixtures/gen/classes/Greyhound.pkl.go @@ -1,20 +1,20 @@ // Code generated from Pkl module `classes`. DO NOT EDIT. package classes -type Greyhound interface { - Dog +type IGreyhound interface { + IDog GetCanRoach() bool } -var _ Greyhound = (*GreyhoundImpl)(nil) +var _ IGreyhound = Greyhound{} -type GreyhoundImpl struct { - *DogImpl +type Greyhound struct { + Dog CanRoach bool `pkl:"canRoach"` } -func (rcv *GreyhoundImpl) GetCanRoach() bool { +func (rcv Greyhound) GetCanRoach() bool { return rcv.CanRoach } diff --git a/pkl/test_fixtures/gen/classes/init.pkl.go b/pkl/test_fixtures/gen/classes/init.pkl.go index 6fd024e..1225d98 100644 --- a/pkl/test_fixtures/gen/classes/init.pkl.go +++ b/pkl/test_fixtures/gen/classes/init.pkl.go @@ -6,7 +6,7 @@ import "github.com/apple/pkl-go/pkl" func init() { pkl.RegisterMapping("classes", Classes{}) pkl.RegisterMapping("classes#House", House{}) - pkl.RegisterMapping("classes#Dog", DogImpl{}) - pkl.RegisterMapping("classes#Greyhound", GreyhoundImpl{}) - pkl.RegisterMapping("classes#Cat", CatImpl{}) + pkl.RegisterMapping("classes#Dog", Dog{}) + pkl.RegisterMapping("classes#Greyhound", Greyhound{}) + pkl.RegisterMapping("classes#Cat", Cat{}) } diff --git a/pkl/test_fixtures/gen/collections/Collections.pkl.go b/pkl/test_fixtures/gen/collections/Collections.pkl.go index 3630fab..0bd737b 100644 --- a/pkl/test_fixtures/gen/collections/Collections.pkl.go +++ b/pkl/test_fixtures/gen/collections/Collections.pkl.go @@ -36,10 +36,10 @@ type Collections struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Collections -func LoadFromPath(ctx context.Context, path string) (ret *Collections, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Collections, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Collections{}, err } defer func() { cerr := evaluator.Close() @@ -52,10 +52,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Collections, err error } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Collections -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Collections, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Collections, error) { var ret Collections if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Collections{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/datasize/Datasize.pkl.go b/pkl/test_fixtures/gen/datasize/Datasize.pkl.go index 3d03f0e..8311399 100644 --- a/pkl/test_fixtures/gen/datasize/Datasize.pkl.go +++ b/pkl/test_fixtures/gen/datasize/Datasize.pkl.go @@ -34,10 +34,10 @@ type Datasize struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Datasize -func LoadFromPath(ctx context.Context, path string) (ret *Datasize, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Datasize, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Datasize{}, err } defer func() { cerr := evaluator.Close() @@ -50,10 +50,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Datasize, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Datasize -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Datasize, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Datasize, error) { var ret Datasize if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Datasize{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/duration/Duration.pkl.go b/pkl/test_fixtures/gen/duration/Duration.pkl.go index e781970..5b7ad4f 100644 --- a/pkl/test_fixtures/gen/duration/Duration.pkl.go +++ b/pkl/test_fixtures/gen/duration/Duration.pkl.go @@ -26,10 +26,10 @@ type Duration struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Duration -func LoadFromPath(ctx context.Context, path string) (ret *Duration, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Duration, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Duration{}, err } defer func() { cerr := evaluator.Close() @@ -42,10 +42,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Duration, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Duration -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Duration, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Duration, error) { var ret Duration if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Duration{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/dynamic/Dynamic.pkl.go b/pkl/test_fixtures/gen/dynamic/Dynamic.pkl.go index d57fd74..4b3a1c2 100644 --- a/pkl/test_fixtures/gen/dynamic/Dynamic.pkl.go +++ b/pkl/test_fixtures/gen/dynamic/Dynamic.pkl.go @@ -12,10 +12,10 @@ type Dynamic struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Dynamic -func LoadFromPath(ctx context.Context, path string) (ret *Dynamic, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Dynamic, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Dynamic{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Dynamic, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Dynamic -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Dynamic, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Dynamic, error) { var ret Dynamic if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Dynamic{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/nullables/Nullables.pkl.go b/pkl/test_fixtures/gen/nullables/Nullables.pkl.go index 6908dee..7ec4cba 100644 --- a/pkl/test_fixtures/gen/nullables/Nullables.pkl.go +++ b/pkl/test_fixtures/gen/nullables/Nullables.pkl.go @@ -70,10 +70,10 @@ type Nullables struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Nullables -func LoadFromPath(ctx context.Context, path string) (ret *Nullables, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Nullables, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Nullables{}, err } defer func() { cerr := evaluator.Close() @@ -86,10 +86,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Nullables, err error) } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Nullables -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Nullables, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Nullables, error) { var ret Nullables if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Nullables{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/primitives/Primitives.pkl.go b/pkl/test_fixtures/gen/primitives/Primitives.pkl.go index 6083eb4..e28428c 100644 --- a/pkl/test_fixtures/gen/primitives/Primitives.pkl.go +++ b/pkl/test_fixtures/gen/primitives/Primitives.pkl.go @@ -38,10 +38,10 @@ type Primitives struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Primitives -func LoadFromPath(ctx context.Context, path string) (ret *Primitives, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Primitives, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Primitives{}, err } defer func() { cerr := evaluator.Close() @@ -54,10 +54,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Primitives, err error) } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Primitives -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Primitives, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Primitives, error) { var ret Primitives if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Primitives{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/unions/Unions.pkl.go b/pkl/test_fixtures/gen/unions/Unions.pkl.go index 43898a9..2c0d099 100644 --- a/pkl/test_fixtures/gen/unions/Unions.pkl.go +++ b/pkl/test_fixtures/gen/unions/Unions.pkl.go @@ -16,10 +16,10 @@ type Unions struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a Unions -func LoadFromPath(ctx context.Context, path string) (ret *Unions, err error) { +func LoadFromPath(ctx context.Context, path string) (ret Unions, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return Unions{}, err } defer func() { cerr := evaluator.Close() @@ -32,10 +32,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *Unions, err error) { } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a Unions -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Unions, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (Unions, error) { var ret Unions if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return Unions{}, err } - return &ret, nil + return ret, nil } diff --git a/pkl/test_fixtures/gen/unknown_type/UnknownType.pkl.go b/pkl/test_fixtures/gen/unknown_type/UnknownType.pkl.go index 414a997..f78c52c 100644 --- a/pkl/test_fixtures/gen/unknown_type/UnknownType.pkl.go +++ b/pkl/test_fixtures/gen/unknown_type/UnknownType.pkl.go @@ -12,10 +12,10 @@ type UnknownType struct { } // LoadFromPath loads the pkl module at the given path and evaluates it into a UnknownType -func LoadFromPath(ctx context.Context, path string) (ret *UnknownType, err error) { +func LoadFromPath(ctx context.Context, path string) (ret UnknownType, err error) { evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) if err != nil { - return nil, err + return UnknownType{}, err } defer func() { cerr := evaluator.Close() @@ -28,10 +28,10 @@ func LoadFromPath(ctx context.Context, path string) (ret *UnknownType, err error } // Load loads the pkl module at the given source and evaluates it with the given evaluator into a UnknownType -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*UnknownType, error) { +func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (UnknownType, error) { var ret UnknownType if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err + return UnknownType{}, err } - return &ret, nil + return ret, nil } From 603e96ed01e438799693b99783fb1925205320d7 Mon Sep 17 00:00:00 2001 From: Thomas Purchas Date: Sun, 30 Jun 2024 20:45:11 +0100 Subject: [PATCH 4/4] Add support for decoding into concreet structs The codegen changes means we need to support decoding data into concreet structs, and not just pointers to structs. --- pkl/decode_struct.go | 32 ++++++++++++++++++++++++-------- pkl/unmarshal_test.go | 14 +++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/pkl/decode_struct.go b/pkl/decode_struct.go index 77dc4ed..506b3bb 100644 --- a/pkl/decode_struct.go +++ b/pkl/decode_struct.go @@ -306,8 +306,15 @@ func getStructFields(typ reflect.Type) map[string]structField { field := typ.Field(i) // embedded if field.Anonymous { - for k, v := range getStructFields(field.Type.Elem()) { - ret[k] = v + switch field.Type.Kind() { + case reflect.Ptr: + for k, v := range getStructFields(field.Type.Elem()) { + ret[k] = v + } + case reflect.Struct: + for k, v := range getStructFields(field.Type) { + ret[k] = v + } } } else { opts := parseStructOpts(&field) @@ -328,13 +335,22 @@ func (d *decoder) getOutputValue(typ reflect.Type) (*reflect.Value, error) { for i := 0; i < numFields; i++ { field := typ.Field(i) if field.Anonymous { - fieldValue := reflect.New(field.Type.Elem()) - // Assertion: all embedded fields are pointers to structs. - structValue, err := d.getOutputValue(field.Type.Elem()) - if err != nil { - return nil, err + var fieldValue reflect.Value + switch field.Type.Kind() { + case reflect.Ptr: + { + fieldValue = reflect.New(field.Type.Elem()) + structValue, err := d.getOutputValue(field.Type.Elem()) + if err != nil { + return nil, err + } + fieldValue.Elem().Set(*structValue) + } + case reflect.Struct: + { + fieldValue = reflect.New(field.Type).Elem() + } } - fieldValue.Elem().Set(*structValue) ret.FieldByName(field.Name).Set(fieldValue) } } diff --git a/pkl/unmarshal_test.go b/pkl/unmarshal_test.go index 0885c14..132c81c 100644 --- a/pkl/unmarshal_test.go +++ b/pkl/unmarshal_test.go @@ -310,29 +310,29 @@ func TestUnmarshal_Dynamic(t *testing.T) { func TestUnmarshal_Classes(t *testing.T) { expected := classes.Classes{ - Animals: []classes.Animal{ - &classes.GreyhoundImpl{ - DogImpl: &classes.DogImpl{ + Animals: []classes.IAnimal{ + &classes.Greyhound{ + Dog: classes.Dog{ Name: "Uni", Barks: false, Breed: "Greyhound", }, CanRoach: true, }, - &classes.CatImpl{ + &classes.Cat{ Name: "Millie", Meows: true, }, }, - MyAnimal: &classes.GreyhoundImpl{ - DogImpl: &classes.DogImpl{ + MyAnimal: &classes.Greyhound{ + Dog: classes.Dog{ Name: "Uni", Barks: false, Breed: "Greyhound", }, CanRoach: true, }, - House: &classes.House{ + House: classes.House{ Area: 2000, Bedrooms: 3, Bathrooms: 2,