Skip to content
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

Allow property assignments to pull values from functions #1545

Merged
merged 9 commits into from
Jun 14, 2021
13 changes: 9 additions & 4 deletions hack/generator/pkg/astbuilder/func_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ func (fn *FuncDetails) AddParameter(id string, parameterType dst.Expr) {
// AddReturns adds (possibly many) return values to the function definition
func (fn *FuncDetails) AddReturns(types ...string) {
for _, t := range types {
field := &dst.Field{
Type: dst.NewIdent(t),
}
fn.Returns = append(fn.Returns, field)
fn.AddReturn(dst.NewIdent(t))
}
}

// AddReturn adds a single return value to the function definition
func (fn *FuncDetails) AddReturn(expr dst.Expr) {
field := &dst.Field{
Type: expr,
}
fn.Returns = append(fn.Returns, field)
}

// AddComments adds multiple comments to the function declaration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
package armconversion

import (
"github.com/dave/dst"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astbuilder"
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
"github.com/dave/dst"
)

// CreateEmptyARMValueFunc represents a function that creates
Expand Down
2 changes: 1 addition & 1 deletion hack/generator/pkg/astmodel/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// Function represents something that is an (unnamed) Go function
type Function interface {
// The unique name of this function
// Name is the unique name of this function
// (You can't have two functions with the same name on the same object or resource)
Name() string

Expand Down
4 changes: 2 additions & 2 deletions hack/generator/pkg/astmodel/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func NewFakeFunction(name string) *FakeFunction {
}
}

var _ Function = &FakeFunction{}

func (fake *FakeFunction) Name() string {
return fake.name
}
Expand Down Expand Up @@ -72,5 +74,3 @@ func (fake *FakeFunction) Equals(f Function) bool {

return true
}

var _ Function = &FakeFunction{}
3 changes: 3 additions & 0 deletions hack/generator/pkg/astmodel/primitive_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var BoolType = &PrimitiveType{"bool", "false"}
// AnyType represents the root Go interface type, permitting any object
var AnyType = &PrimitiveType{"interface{}", "nil"}

// ErrorType represents the standard Go error interface
var ErrorType = &PrimitiveType{"error", "nil"}

// assert that we implemented Type correctly
var _ Type = (*PrimitiveType)(nil)

Expand Down
14 changes: 14 additions & 0 deletions hack/generator/pkg/astmodel/value_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package astmodel

// ValueFunction defines a function that returns a single value suitable for storing in a property
type ValueFunction interface {
Function

// ReturnType specifies the return type of the function
ReturnType() Type
}
100 changes: 100 additions & 0 deletions hack/generator/pkg/conversions/fake_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/

package conversions

import (
"github.com/dave/dst"

"github.com/Azure/azure-service-operator/hack/generator/pkg/astbuilder"
"github.com/Azure/azure-service-operator/hack/generator/pkg/astmodel"
)

// FakeFunction is a fake function that can be used for testing purposes
type FakeFunction struct {
name string
Imported *astmodel.PackageReferenceSet
Referenced astmodel.TypeNameSet
returnType astmodel.Type
idFactory astmodel.IdentifierFactory
}

func NewFakeFunction(name string, idFactory astmodel.IdentifierFactory) *FakeFunction {
return &FakeFunction{
name: name,
idFactory: idFactory,
Imported: astmodel.NewPackageReferenceSet(),
}
}

var _ astmodel.Function = &FakeFunction{}

var _ astmodel.ValueFunction = &FakeFunction{}

func (fake *FakeFunction) Name() string {
return fake.name
}

func (fake *FakeFunction) RequiredPackageReferences() *astmodel.PackageReferenceSet {
result := astmodel.NewPackageReferenceSet()
result.Merge(fake.Imported)
return result
}

func (fake *FakeFunction) References() astmodel.TypeNameSet {
return fake.Referenced
}

func (fake *FakeFunction) AsFunc(generationContext *astmodel.CodeGenerationContext, reciever astmodel.TypeName) *dst.FuncDecl {
recieverName := fake.idFactory.CreateIdentifier(reciever.Name(), astmodel.NotExported)
details := astbuilder.FuncDetails{
ReceiverIdent: recieverName,
ReceiverType: astmodel.NewOptionalType(reciever).AsType(generationContext),
Name: fake.name,
}

if fake.returnType != nil {
details.AddReturn(fake.returnType.AsType(generationContext))
}

return details.DefineFunc()
}

func (fake *FakeFunction) Equals(f astmodel.Function) bool {
if fake == nil && f == nil {
return true
}

if fake == nil || f == nil {
return false
}

fn, ok := f.(*FakeFunction)
if !ok {
return false
}

// Check to see if they have the same references
if !fake.Referenced.Equals(fn.Referenced) {
return false
}

// Check to see if they have the same imports
if fake.Imported.Length() != fn.Imported.Length() {
return false
}

for _, imp := range fake.Imported.AsSlice() {
if !fn.Imported.Contains(imp) {
return false
}
}

return true
}

func (fake *FakeFunction) ReturnType() astmodel.Type {
return fake.returnType
}
Loading