From 4d455c6390c0799e0f36593a0622839c6a5e67a4 Mon Sep 17 00:00:00 2001 From: Nic Cope Date: Thu, 17 Oct 2019 15:27:38 -0700 Subject: [PATCH] Support simple resource class selection https://github.com/crossplaneio/crossplane/pull/926 https://github.com/crossplaneio/crossplane/pull/927 https://github.com/crossplaneio/crossplane-runtime/pull/48 Updates angryjet to reflect the above changes. Signed-off-by: Nic Cope --- cmd/angryjet/main.go | 91 +++++++---------------- internal/fields/fields.go | 61 +++++++--------- internal/match/match.go | 36 ++-------- internal/method/method.go | 117 ++++++++++++++---------------- internal/method/method_test.go | 128 ++++++++++++++++----------------- 5 files changed, 167 insertions(+), 266 deletions(-) diff --git a/cmd/angryjet/main.go b/cmd/angryjet/main.go index 2868941..89a4727 100644 --- a/cmd/angryjet/main.go +++ b/cmd/angryjet/main.go @@ -46,24 +46,23 @@ const ( CoreAlias = "corev1" CoreImport = "k8s.io/api/core/v1" + MetaAlias = "metav1" + MetaImport = "k8s.io/apimachinery/pkg/apis/meta/v1" + RuntimeAlias = "runtimev1alpha1" RuntimeImport = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1" - - ResourceImport = "github.com/crossplaneio/crossplane-runtime/pkg/resource" ) func main() { var ( app = kingpin.New(filepath.Base(os.Args[0]), "Generates Crossplane API type methods.").DefaultEnvars() - methodsets = app.Command("generate-methodsets", "Generate a Crossplane method sets.") - headerFile = methodsets.Flag("header-file", "The contents of this file will be added to the top of all generated files.").ExistingFile() - filenameManaged = methodsets.Flag("filename-managed", "The filename of generated managed resource files.").Default("zz_generated.managed.go").String() - filenameClaim = methodsets.Flag("filename-claim", "The filename of generated resource claim files.").Default("zz_generated.claim.go").String() - filenamePortableClass = methodsets.Flag("filename-portable-class", "The filename of generated portable class files.").Default("zz_generated.portableclass.go").String() - filenamePortableClassList = methodsets.Flag("filename-portable-class-list", "The filename of generated portable class list files.").Default("zz_generated.portableclasslist.go").String() - filenameNonPortableClass = methodsets.Flag("filename-non-portable-class", "The filename of generated non-portable class files.").Default("zz_generated.nonportableclass.go").String() - pattern = methodsets.Arg("packages", "Package(s) for which to generate methods, for example github.com/crossplaneio/crossplane/apis/...").String() + methodsets = app.Command("generate-methodsets", "Generate a Crossplane method sets.") + headerFile = methodsets.Flag("header-file", "The contents of this file will be added to the top of all generated files.").ExistingFile() + filenameManaged = methodsets.Flag("filename-managed", "The filename of generated managed resource files.").Default("zz_generated.managed.go").String() + filenameClaim = methodsets.Flag("filename-claim", "The filename of generated resource claim files.").Default("zz_generated.claim.go").String() + filenameClass = methodsets.Flag("filename-class", "The filename of generated resource class files.").Default("zz_generated.class.go").String() + pattern = methodsets.Arg("packages", "Package(s) for which to generate methods, for example github.com/crossplaneio/crossplane/apis/...").String() ) kingpin.MustParse(app.Parse(os.Args[1:])) @@ -83,9 +82,7 @@ func main() { } kingpin.FatalIfError(GenerateManaged(*filenameManaged, header, p), "cannot write managed resource method set for package %s", p.PkgPath) kingpin.FatalIfError(GenerateClaim(*filenameClaim, header, p), "cannot write resource claim method set for package %s", p.PkgPath) - kingpin.FatalIfError(GeneratePortableClass(*filenamePortableClass, header, p), "cannot write portable class method set for package %s", p.PkgPath) - kingpin.FatalIfError(GeneratePortableClassList(*filenamePortableClassList, header, p), "cannot write portable class list method set for package %s", p.PkgPath) - kingpin.FatalIfError(GenerateNonPortableClass(*filenameNonPortableClass, header, p), "cannot write non portable class method set for package %s", p.PkgPath) + kingpin.FatalIfError(GenerateClass(*filenameClass, header, p), "cannot write resource class method set for package %s", p.PkgPath) } } @@ -100,10 +97,10 @@ func GenerateManaged(filename, header string, p *packages.Package) error { "GetBindingPhase": method.NewGetBindingPhase(receiver, RuntimeImport), "SetClaimReference": method.NewSetClaimReference(receiver, CoreImport), "GetClaimReference": method.NewGetClaimReference(receiver, CoreImport), - "SetNonPortableClassReference": method.NewSetNonPortableClassReference(receiver, CoreImport), - "GetNonPortableClassReference": method.NewGetNonPortableClassReference(receiver, CoreImport), - "SetWriteConnectionSecretToReference": method.NewSetWriteConnectionSecretToReference(receiver, CoreImport), - "GetWriteConnectionSecretToReference": method.NewGetWriteConnectionSecretToReference(receiver, CoreImport), + "SetClassReference": method.NewSetClassReference(receiver, CoreImport), + "GetClassReference": method.NewGetClassReference(receiver, CoreImport), + "SetWriteConnectionSecretToReference": method.NewSetWriteConnectionSecretToReference(receiver, RuntimeImport), + "GetWriteConnectionSecretToReference": method.NewGetWriteConnectionSecretToReference(receiver, RuntimeImport), "SetReclaimPolicy": method.NewSetReclaimPolicy(receiver, RuntimeImport, fields.NameSpec), "GetReclaimPolicy": method.NewGetReclaimPolicy(receiver, RuntimeImport, fields.NameSpec), } @@ -132,18 +129,21 @@ func GenerateClaim(filename, header string, p *packages.Package) error { "GetCondition": method.NewGetCondition(receiver, RuntimeImport), "SetBindingPhase": method.NewSetBindingPhase(receiver, RuntimeImport), "GetBindingPhase": method.NewGetBindingPhase(receiver, RuntimeImport), + "SetClassSelector": method.NewSetClassSelector(receiver, MetaImport), + "GetClassSelector": method.NewGetClassSelector(receiver, MetaImport), + "SetClassReference": method.NewSetClassReference(receiver, CoreImport), + "GetClassReference": method.NewGetClassReference(receiver, CoreImport), "SetResourceReference": method.NewSetResourceReference(receiver, CoreImport), "GetResourceReference": method.NewGetResourceReference(receiver, CoreImport), - "SetPortableClassReference": method.NewSetPortableClassReference(receiver, CoreImport), - "GetPortableClassReference": method.NewGetPortableClassReference(receiver, CoreImport), - "SetWriteConnectionSecretToReference": method.NewSetWriteConnectionSecretToReference(receiver, CoreImport), - "GetWriteConnectionSecretToReference": method.NewGetWriteConnectionSecretToReference(receiver, CoreImport), + "SetWriteConnectionSecretToReference": method.NewLocalSetWriteConnectionSecretToReference(receiver, RuntimeImport), + "GetWriteConnectionSecretToReference": method.NewLocalGetWriteConnectionSecretToReference(receiver, RuntimeImport), } err := generate.WriteMethods(p, methods, filepath.Join(filepath.Dir(p.GoFiles[0]), filename), generate.WithHeaders(header), generate.WithImportAliases(map[string]string{ CoreImport: CoreAlias, + MetaImport: MetaAlias, RuntimeImport: RuntimeAlias, }), generate.WithMatcher(match.AllOf( @@ -155,49 +155,8 @@ func GenerateClaim(filename, header string, p *packages.Package) error { return errors.Wrap(err, "cannot write resource claim methods") } -// GeneratePortableClass generates the resource.PortableClass method set. -func GeneratePortableClass(filename, header string, p *packages.Package) error { - receiver := "cs" - - methods := method.Set{ - "SetNonPortableClassReference": method.NewSetNonPortableClassReference(receiver, CoreImport), - "GetNonPortableClassReference": method.NewGetNonPortableClassReference(receiver, CoreImport), - } - - err := generate.WriteMethods(p, methods, filepath.Join(filepath.Dir(p.GoFiles[0]), filename), - generate.WithHeaders(header), - generate.WithImportAliases(map[string]string{CoreImport: CoreAlias}), - generate.WithMatcher(match.AllOf( - match.PortableClass(), - match.DoesNotHaveMarker(comments.In(p), DisableMarker, "false")), - ), - ) - - return errors.Wrap(err, "cannot write portable class methods") -} - -// GeneratePortableClassList generates the resource.PortableClassList method set. -func GeneratePortableClassList(filename, header string, p *packages.Package) error { - receiver := "csl" - - methods := method.Set{ - "SetPortableClassItems": method.NewSetPortableClassItems(receiver, ResourceImport), - "GetPortableClassItems": method.NewGetPortableClassItems(receiver, ResourceImport), - } - - err := generate.WriteMethods(p, methods, filepath.Join(filepath.Dir(p.GoFiles[0]), filename), - generate.WithHeaders(header), - generate.WithMatcher(match.AllOf( - match.PortableClassList(), - match.DoesNotHaveMarker(comments.In(p), DisableMarker, "false")), - ), - ) - - return errors.Wrap(err, "cannot write portable class methods") -} - -// GenerateNonPortableClass generates the resource.NonPortableClass method set. -func GenerateNonPortableClass(filename, header string, p *packages.Package) error { +// GenerateClass generates the resource.Class method set. +func GenerateClass(filename, header string, p *packages.Package) error { receiver := "cs" methods := method.Set{ @@ -209,10 +168,10 @@ func GenerateNonPortableClass(filename, header string, p *packages.Package) erro generate.WithHeaders(header), generate.WithImportAliases(map[string]string{RuntimeImport: RuntimeAlias}), generate.WithMatcher(match.AllOf( - match.NonPortableClass(), + match.Class(), match.DoesNotHaveMarker(comments.In(p), DisableMarker, "false")), ), ) - return errors.Wrap(err, "cannot write non-portable class methods") + return errors.Wrap(err, "cannot write resource class methods") } diff --git a/internal/fields/fields.go b/internal/fields/fields.go index 51e9cb2..25c6ef0 100644 --- a/internal/fields/fields.go +++ b/internal/fields/fields.go @@ -24,34 +24,32 @@ import ( // Field names. const ( - NameTypeMeta = "TypeMeta" - NameObjectMeta = "ObjectMeta" - NameListMeta = "ListMeta" - NameSpec = "Spec" - NameSpecTemplate = "SpecTemplate" - NameStatus = "Status" - NameResourceSpec = "ResourceSpec" - NameResourceStatus = "ResourceStatus" - NameResourceClaimSpec = "ResourceClaimSpec" - NameNonPortableClassSpecTemplate = "NonPortableClassSpecTemplate" - NamePortableClass = "PortableClass" - NameItems = "Items" + NameTypeMeta = "TypeMeta" + NameObjectMeta = "ObjectMeta" + NameListMeta = "ListMeta" + NameSpec = "Spec" + NameSpecTemplate = "SpecTemplate" + NameStatus = "Status" + NameResourceSpec = "ResourceSpec" + NameResourceStatus = "ResourceStatus" + NameResourceClaimSpec = "ResourceClaimSpec" + NameClassSpecTemplate = "ClassSpecTemplate" + NameItems = "Items" ) // Field type suffixes. const ( - TypeSuffixTypeMeta = "k8s.io/apimachinery/pkg/apis/meta/v1.TypeMeta" - TypeSuffixObjectMeta = "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta" - TypeSuffixListMeta = "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta" - TypeSuffixSpec = NameSpec - TypeSuffixSpecTemplate = NameSpecTemplate - TypeSuffixStatus = NameStatus - TypeSuffixResourceSpec = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceSpec" - TypeSuffixResourceStatus = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceStatus" - TypeSuffixResourceClaimSpec = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceClaimSpec" - TypeSuffixResourceClaimStatus = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceClaimStatus" - TypeSuffixNonPortableClassSpecTemplate = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.NonPortableClassSpecTemplate" - TypeSuffixPortableClass = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.PortableClass" + TypeSuffixTypeMeta = "k8s.io/apimachinery/pkg/apis/meta/v1.TypeMeta" + TypeSuffixObjectMeta = "k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta" + TypeSuffixListMeta = "k8s.io/apimachinery/pkg/apis/meta/v1.ListMeta" + TypeSuffixSpec = NameSpec + TypeSuffixSpecTemplate = NameSpecTemplate + TypeSuffixStatus = NameStatus + TypeSuffixResourceSpec = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceSpec" + TypeSuffixResourceStatus = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceStatus" + TypeSuffixResourceClaimSpec = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceClaimSpec" + TypeSuffixResourceClaimStatus = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ResourceClaimStatus" + TypeSuffixClassSpecTemplate = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1.ClassSpecTemplate" ) func matches(s *types.Struct, m Matcher) bool { @@ -199,17 +197,10 @@ func IsResourceClaimStatus() Matcher { return IsTypeNamed(TypeSuffixResourceClaimStatus, NameStatus) } -// IsNonPortableClassSpecTemplate returns a Matcher that returns true if the -// supplied field appears to be a Crossplane non-portable resource class spec -// template. -func IsNonPortableClassSpecTemplate() Matcher { - return IsTypeNamed(TypeSuffixNonPortableClassSpecTemplate, NameNonPortableClassSpecTemplate) -} - -// IsPortableClass returns a Matcher that returns true if the supplied field -// appears to be a Crossplane portable resource class. -func IsPortableClass() Matcher { - return IsTypeNamed(TypeSuffixPortableClass, NamePortableClass) +// IsClassSpecTemplate returns a Matcher that returns true if the supplied field +// appears to be a Crossplane resource class spec template. +func IsClassSpecTemplate() Matcher { + return IsTypeNamed(TypeSuffixClassSpecTemplate, NameClassSpecTemplate) } // IsItems returns a Matcher that returns true if the supplied field appears to diff --git a/internal/match/match.go b/internal/match/match.go index 6309cae..c9b9ae7 100644 --- a/internal/match/match.go +++ b/internal/match/match.go @@ -45,15 +45,15 @@ func Managed() Object { } } -// NonPortableClass returns an Object matcher that returns true if the supplied -// Object is a Crossplane non-portable resource class. -func NonPortableClass() Object { +// Class returns an Object matcher that returns true if the supplied Object is a +// Crossplane resource class. +func Class() Object { return func(o types.Object) bool { return fields.Has(o, fields.IsTypeMeta().And(fields.IsEmbedded()), fields.IsObjectMeta().And(fields.IsEmbedded()), fields.IsSpecTemplate().And(fields.HasFieldThat( - fields.IsNonPortableClassSpecTemplate().And(fields.IsEmbedded()), + fields.IsClassSpecTemplate().And(fields.IsEmbedded()), )), ) } @@ -74,34 +74,6 @@ func Claim() Object { } } -// PortableClass returns an Object matcher that returns true if the supplied -// Object is a Crossplane portable resource class. -func PortableClass() Object { - return func(o types.Object) bool { - return fields.Has(o, - fields.IsTypeMeta().And(fields.IsEmbedded()), - fields.IsObjectMeta().And(fields.IsEmbedded()), - fields.IsPortableClass().And(fields.IsEmbedded()), - ) - } -} - -// PortableClassList returns an Object matcher that returns true if the supplied -// Object is a Crossplane portable resource class list. -func PortableClassList() Object { - return func(o types.Object) bool { - return fields.Has(o, - fields.IsTypeMeta().And(fields.IsEmbedded()), - fields.IsListMeta().And(fields.IsEmbedded()), - fields.IsItems().And(fields.IsSlice()).And(fields.HasFieldThat( - fields.IsTypeMeta().And(fields.IsEmbedded()), - fields.IsObjectMeta().And(fields.IsEmbedded()), - fields.IsPortableClass().And(fields.IsEmbedded()), - )), - ) - } -} - // HasMarker returns an Object matcher that returns true if the supplied Object // has a comment marker k with the value v. Comment markers are read from the // supplied Comments. diff --git a/internal/method/method.go b/internal/method/method.go index c86ff83..a94076d 100644 --- a/internal/method/method.go +++ b/internal/method/method.go @@ -21,7 +21,6 @@ import ( "go/token" "go/types" "sort" - "strings" "github.com/dave/jennifer/jen" @@ -165,50 +164,46 @@ func NewGetResourceReference(receiver, core string) New { } } -// NewSetNonPortableClassReference returns a NewMethod that writes a -// SetNonPortableClassReference method for the supplied Object to the supplied -// file. -func NewSetNonPortableClassReference(receiver, core string) New { +// NewSetClassSelector returns a NewMethod that writes a SetClassSelector +// method for the supplied Object to the supplied file. +func NewSetClassSelector(receiver, meta string) New { return func(f *jen.File, o types.Object) { - f.Commentf("SetNonPortableClassReference of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetNonPortableClassReference").Params(jen.Id("r").Op("*").Qual(core, "ObjectReference")).Block( - jen.Id(receiver).Dot(fields.NameSpec).Dot("NonPortableClassReference").Op("=").Id("r"), + f.Commentf("SetClassSelector of this %s.", o.Name()) + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetClassSelector").Params(jen.Id("s").Op("*").Qual(meta, "LabelSelector")).Block( + jen.Id(receiver).Dot(fields.NameSpec).Dot("ClassSelector").Op("=").Id("s"), ) } } -// NewGetNonPortableClassReference returns a NewMethod that writes a -// GetNonPortableClassReference method for the supplied Object to the supplied -// file. -func NewGetNonPortableClassReference(receiver, core string) New { +// NewGetClassSelector returns a NewMethod that writes a GetClassSelector +// method for the supplied Object to the supplied file. +func NewGetClassSelector(receiver, meta string) New { return func(f *jen.File, o types.Object) { - f.Commentf("GetNonPortableClassReference of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetNonPortableClassReference").Params().Op("*").Qual(core, "ObjectReference").Block( - jen.Return(jen.Id(receiver).Dot(fields.NameSpec).Dot("NonPortableClassReference")), + f.Commentf("GetClassSelector of this %s.", o.Name()) + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetClassSelector").Params().Op("*").Qual(meta, "LabelSelector").Block( + jen.Return(jen.Id(receiver).Dot(fields.NameSpec).Dot("ClassSelector")), ) } } -// NewSetPortableClassReference returns a NewMethod that writes a -// SetPortableClassReference method for the supplied Object to the supplied -// file. -func NewSetPortableClassReference(receiver, core string) New { +// NewSetClassReference returns a NewMethod that writes a SetClassReference +// method for the supplied Object to the supplied file. +func NewSetClassReference(receiver, core string) New { return func(f *jen.File, o types.Object) { - f.Commentf("SetPortableClassReference of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetPortableClassReference").Params(jen.Id("r").Op("*").Qual(core, "LocalObjectReference")).Block( - jen.Id(receiver).Dot(fields.NameSpec).Dot("PortableClassReference").Op("=").Id("r"), + f.Commentf("SetClassReference of this %s.", o.Name()) + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetClassReference").Params(jen.Id("r").Op("*").Qual(core, "ObjectReference")).Block( + jen.Id(receiver).Dot(fields.NameSpec).Dot("ClassReference").Op("=").Id("r"), ) } } -// NewGetPortableClassReference returns a NewMethod that writes a -// GetPortableClassReference method for the supplied Object to the supplied -// file. -func NewGetPortableClassReference(receiver, core string) New { +// NewGetClassReference returns a NewMethod that writes a GetClassReference +// method for the supplied Object to the supplied file. +func NewGetClassReference(receiver, core string) New { return func(f *jen.File, o types.Object) { - f.Commentf("GetPortableClassReference of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetPortableClassReference").Params().Op("*").Qual(core, "LocalObjectReference").Block( - jen.Return(jen.Id(receiver).Dot(fields.NameSpec).Dot("PortableClassReference")), + f.Commentf("GetClassReference of this %s.", o.Name()) + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetClassReference").Params().Op("*").Qual(core, "ObjectReference").Block( + jen.Return(jen.Id(receiver).Dot(fields.NameSpec).Dot("ClassReference")), ) } } @@ -216,10 +211,10 @@ func NewGetPortableClassReference(receiver, core string) New { // NewSetWriteConnectionSecretToReference returns a NewMethod that writes a // SetWriteConnectionSecretToReference method for the supplied Object to the // supplied file. -func NewSetWriteConnectionSecretToReference(receiver, core string) New { +func NewSetWriteConnectionSecretToReference(receiver, runtime string) New { return func(f *jen.File, o types.Object) { f.Commentf("SetWriteConnectionSecretToReference of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetWriteConnectionSecretToReference").Params(jen.Id("r").Qual(core, "LocalObjectReference")).Block( + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetWriteConnectionSecretToReference").Params(jen.Id("r").Op("*").Qual(runtime, "SecretReference")).Block( jen.Id(receiver).Dot(fields.NameSpec).Dot("WriteConnectionSecretToReference").Op("=").Id("r"), ) } @@ -228,10 +223,34 @@ func NewSetWriteConnectionSecretToReference(receiver, core string) New { // NewGetWriteConnectionSecretToReference returns a NewMethod that writes a // GetWriteConnectionSecretToReference method for the supplied Object to the // supplied file. -func NewGetWriteConnectionSecretToReference(receiver, core string) New { +func NewGetWriteConnectionSecretToReference(receiver, runtime string) New { + return func(f *jen.File, o types.Object) { + f.Commentf("GetWriteConnectionSecretToReference of this %s.", o.Name()) + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetWriteConnectionSecretToReference").Params().Op("*").Qual(runtime, "SecretReference").Block( + jen.Return(jen.Id(receiver).Dot(fields.NameSpec).Dot("WriteConnectionSecretToReference")), + ) + } +} + +// NewLocalSetWriteConnectionSecretToReference returns a NewMethod that writes a +// SetWriteConnectionSecretToReference method for the supplied Object to the +// supplied file. +func NewLocalSetWriteConnectionSecretToReference(receiver, runtime string) New { + return func(f *jen.File, o types.Object) { + f.Commentf("SetWriteConnectionSecretToReference of this %s.", o.Name()) + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetWriteConnectionSecretToReference").Params(jen.Id("r").Op("*").Qual(runtime, "LocalSecretReference")).Block( + jen.Id(receiver).Dot(fields.NameSpec).Dot("WriteConnectionSecretToReference").Op("=").Id("r"), + ) + } +} + +// NewLocalGetWriteConnectionSecretToReference returns a NewMethod that writes a +// GetWriteConnectionSecretToReference method for the supplied Object to the +// supplied file. +func NewLocalGetWriteConnectionSecretToReference(receiver, runtime string) New { return func(f *jen.File, o types.Object) { f.Commentf("GetWriteConnectionSecretToReference of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetWriteConnectionSecretToReference").Params().Qual(core, "LocalObjectReference").Block( + f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetWriteConnectionSecretToReference").Params().Op("*").Qual(runtime, "LocalSecretReference").Block( jen.Return(jen.Id(receiver).Dot(fields.NameSpec).Dot("WriteConnectionSecretToReference")), ) } @@ -260,35 +279,3 @@ func NewGetReclaimPolicy(receiver, runtime, field string) New { ) } } - -// NewSetPortableClassItems returns a NewMethod that writes a -// SetPortableClassItems method for the supplied Object to the supplied file. -func NewSetPortableClassItems(receiver, resource string) New { - return func(f *jen.File, o types.Object) { - element := strings.TrimSuffix(o.Name(), "List") - f.Commentf("SetPortableClassItems of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("SetPortableClassItems").Params(jen.Id("i").Index().Qual(resource, "PortableClass")).Block( - jen.Id(receiver).Dot("Items").Op("=").Make(jen.Index().Id(element), jen.Id("0"), jen.Len(jen.Id("i"))), - jen.For(jen.Id("j").Op(":=").Range().Id("i")).Block( - jen.If(jen.List(jen.Id("actual"), jen.Id("ok")).Op(":=").Id("i").Index(jen.Id("j")).Assert(jen.Op("*").Id(element)), jen.Id("ok")).Block( - jen.Id(receiver).Dot("Items").Op("=").Append(jen.Id(receiver).Dot("Items"), jen.Op("*").Id("actual")), - ), - ), - ) - } -} - -// NewGetPortableClassItems returns a NewMethod that writes a -// GetPortableClassItems method for the supplied Object to the supplied file. -func NewGetPortableClassItems(receiver, resource string) New { - return func(f *jen.File, o types.Object) { - f.Commentf("GetPortableClassItems of this %s.", o.Name()) - f.Func().Params(jen.Id(receiver).Op("*").Id(o.Name())).Id("GetPortableClassItems").Params().Index().Qual(resource, "PortableClass").Block( - jen.Id("items").Op(":=").Make(jen.Index().Qual(resource, "PortableClass"), jen.Len(jen.Id(receiver).Dot("Items"))), - jen.For(jen.Id("i").Op(":=").Range().Id(receiver).Dot("Items")).Block( - jen.Id("items").Index(jen.Id("i")).Op("=").Qual(resource, "PortableClass").Call(jen.Op("&").Id(receiver).Dot("Items").Index(jen.Id("i"))), - ), - jen.Return(jen.Id("items")), - ) - } -} diff --git a/internal/method/method_test.go b/internal/method/method_test.go index f8a0b3d..38a8a48 100644 --- a/internal/method/method_test.go +++ b/internal/method/method_test.go @@ -172,85 +172,86 @@ func (t *Type) GetResourceReference() *core.ObjectReference { } } -func TestNewSetNonPortableClassReference(t *testing.T) { +func TestNewSetClassSelector(t *testing.T) { want := `package pkg -import core "example.org/core" +import meta "example.org/meta" -// SetNonPortableClassReference of this Type. -func (t *Type) SetNonPortableClassReference(r *core.ObjectReference) { - t.Spec.NonPortableClassReference = r +// SetClassSelector of this Type. +func (t *Type) SetClassSelector(s *meta.LabelSelector) { + t.Spec.ClassSelector = s } ` f := jen.NewFile("pkg") - NewSetNonPortableClassReference("t", "example.org/core")(f, MockObject{Named: "Type"}) + NewSetClassSelector("t", "example.org/meta")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewSetNonPortableClassReference(): -want, +got\n%s", diff) + t.Errorf("NewSetClassSelector(): -want, +got\n%s", diff) } } -func TestNewGetNonPortableClassReference(t *testing.T) { +func TestNewGetClassSelector(t *testing.T) { want := `package pkg -import core "example.org/core" +import meta "example.org/meta" -// GetNonPortableClassReference of this Type. -func (t *Type) GetNonPortableClassReference() *core.ObjectReference { - return t.Spec.NonPortableClassReference +// GetClassSelector of this Type. +func (t *Type) GetClassSelector() *meta.LabelSelector { + return t.Spec.ClassSelector } ` f := jen.NewFile("pkg") - NewGetNonPortableClassReference("t", "example.org/core")(f, MockObject{Named: "Type"}) + NewGetClassSelector("t", "example.org/meta")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewGetNonPortableClassReference(): -want, +got\n%s", diff) + t.Errorf("NewGetClassSelector(): -want, +got\n%s", diff) } } -func TestNewSetPortableClassReference(t *testing.T) { + +func TestNewSetClassReference(t *testing.T) { want := `package pkg import core "example.org/core" -// SetPortableClassReference of this Type. -func (t *Type) SetPortableClassReference(r *core.LocalObjectReference) { - t.Spec.PortableClassReference = r +// SetClassReference of this Type. +func (t *Type) SetClassReference(r *core.ObjectReference) { + t.Spec.ClassReference = r } ` f := jen.NewFile("pkg") - NewSetPortableClassReference("t", "example.org/core")(f, MockObject{Named: "Type"}) + NewSetClassReference("t", "example.org/core")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewSetPortableClassReference(): -want, +got\n%s", diff) + t.Errorf("NewSetClassReference(): -want, +got\n%s", diff) } } -func TestNewGetPortableClassReference(t *testing.T) { +func TestNewGetClassReference(t *testing.T) { want := `package pkg import core "example.org/core" -// GetPortableClassReference of this Type. -func (t *Type) GetPortableClassReference() *core.LocalObjectReference { - return t.Spec.PortableClassReference +// GetClassReference of this Type. +func (t *Type) GetClassReference() *core.ObjectReference { + return t.Spec.ClassReference } ` f := jen.NewFile("pkg") - NewGetPortableClassReference("t", "example.org/core")(f, MockObject{Named: "Type"}) + NewGetClassReference("t", "example.org/core")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewGetPortableClassReference(): -want, +got\n%s", diff) + t.Errorf("NewGetClassReference(): -want, +got\n%s", diff) } } func TestNewSetWriteConnectionSecretToReference(t *testing.T) { want := `package pkg -import core "example.org/core" +import runtime "example.org/runtime" // SetWriteConnectionSecretToReference of this Type. -func (t *Type) SetWriteConnectionSecretToReference(r core.LocalObjectReference) { +func (t *Type) SetWriteConnectionSecretToReference(r *runtime.SecretReference) { t.Spec.WriteConnectionSecretToReference = r } ` f := jen.NewFile("pkg") - NewSetWriteConnectionSecretToReference("t", "example.org/core")(f, MockObject{Named: "Type"}) + NewSetWriteConnectionSecretToReference("t", "example.org/runtime")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { t.Errorf("NewSetWriteConnectionSecretToReference(): -want, +got\n%s", diff) } @@ -259,93 +260,84 @@ func (t *Type) SetWriteConnectionSecretToReference(r core.LocalObjectReference) func TestNewGetWriteConnectionSecretToReference(t *testing.T) { want := `package pkg -import core "example.org/core" +import runtime "example.org/runtime" // GetWriteConnectionSecretToReference of this Type. -func (t *Type) GetWriteConnectionSecretToReference() core.LocalObjectReference { +func (t *Type) GetWriteConnectionSecretToReference() *runtime.SecretReference { return t.Spec.WriteConnectionSecretToReference } ` f := jen.NewFile("pkg") - NewGetWriteConnectionSecretToReference("t", "example.org/core")(f, MockObject{Named: "Type"}) + NewGetWriteConnectionSecretToReference("t", "example.org/runtime")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewGetWriteConnectionSecretToReference(): -want, +got\n%s", diff) + t.Errorf("NewGetWriteConnectionSecretToLocalReference(): -want, +got\n%s", diff) } } -func TestNewSetReclaimPolicy(t *testing.T) { +func TestNewLocalSetWriteConnectionSecretToReference(t *testing.T) { want := `package pkg import runtime "example.org/runtime" -// SetReclaimPolicy of this Type. -func (t *Type) SetReclaimPolicy(r runtime.ReclaimPolicy) { - t.Spec.ReclaimPolicy = r +// SetWriteConnectionSecretToReference of this Type. +func (t *Type) SetWriteConnectionSecretToReference(r *runtime.LocalSecretReference) { + t.Spec.WriteConnectionSecretToReference = r } ` f := jen.NewFile("pkg") - NewSetReclaimPolicy("t", "example.org/runtime", fields.NameSpec)(f, MockObject{Named: "Type"}) + NewLocalSetWriteConnectionSecretToReference("t", "example.org/runtime")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewSetReclaimPolicy(): -want, +got\n%s", diff) + t.Errorf("NewSetWriteConnectionSecretToLocalReference(): -want, +got\n%s", diff) } } -func TestNewGetReclaimPolicy(t *testing.T) { +func TestNewLocalGetWriteConnectionSecretToReference(t *testing.T) { want := `package pkg import runtime "example.org/runtime" -// GetReclaimPolicy of this Type. -func (t *Type) GetReclaimPolicy() runtime.ReclaimPolicy { - return t.Spec.ReclaimPolicy +// GetWriteConnectionSecretToReference of this Type. +func (t *Type) GetWriteConnectionSecretToReference() *runtime.LocalSecretReference { + return t.Spec.WriteConnectionSecretToReference } ` f := jen.NewFile("pkg") - NewGetReclaimPolicy("t", "example.org/runtime", fields.NameSpec)(f, MockObject{Named: "Type"}) + NewLocalGetWriteConnectionSecretToReference("t", "example.org/runtime")(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewGetReclaimPolicy(): -want, +got\n%s", diff) + t.Errorf("NewGetWriteConnectionSecretToReference(): -want, +got\n%s", diff) } } -func TestNewSetPortableClassItems(t *testing.T) { +func TestNewSetReclaimPolicy(t *testing.T) { want := `package pkg -import resource "example.org/resource" +import runtime "example.org/runtime" -// SetPortableClassItems of this TypeList. -func (tl *TypeList) SetPortableClassItems(i []resource.PortableClass) { - tl.Items = make([]Type, 0, len(i)) - for j := range i { - if actual, ok := i[j].(*Type); ok { - tl.Items = append(tl.Items, *actual) - } - } +// SetReclaimPolicy of this Type. +func (t *Type) SetReclaimPolicy(r runtime.ReclaimPolicy) { + t.Spec.ReclaimPolicy = r } ` f := jen.NewFile("pkg") - NewSetPortableClassItems("tl", "example.org/resource")(f, MockObject{Named: "TypeList"}) + NewSetReclaimPolicy("t", "example.org/runtime", fields.NameSpec)(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewSetPortableClassItems(): -want, +got\n%s", diff) + t.Errorf("NewSetReclaimPolicy(): -want, +got\n%s", diff) } } -func TestNewGetPortableClassItems(t *testing.T) { +func TestNewGetReclaimPolicy(t *testing.T) { want := `package pkg -import resource "example.org/resource" +import runtime "example.org/runtime" -// GetPortableClassItems of this Type. -func (t *Type) GetPortableClassItems() []resource.PortableClass { - items := make([]resource.PortableClass, len(t.Items)) - for i := range t.Items { - items[i] = resource.PortableClass(&t.Items[i]) - } - return items +// GetReclaimPolicy of this Type. +func (t *Type) GetReclaimPolicy() runtime.ReclaimPolicy { + return t.Spec.ReclaimPolicy } ` f := jen.NewFile("pkg") - NewGetPortableClassItems("t", "example.org/resource")(f, MockObject{Named: "Type"}) + NewGetReclaimPolicy("t", "example.org/runtime", fields.NameSpec)(f, MockObject{Named: "Type"}) if diff := cmp.Diff(want, fmt.Sprintf("%#v", f)); diff != "" { - t.Errorf("NewGetPortableClassItems(): -want, +got\n%s", diff) + t.Errorf("NewGetReclaimPolicy(): -want, +got\n%s", diff) } }