Skip to content

Commit

Permalink
Support simple resource class selection
Browse files Browse the repository at this point in the history
crossplane/crossplane#926
crossplane/crossplane#927
crossplane/crossplane-runtime#48

Updates angryjet to reflect the above changes.

Signed-off-by: Nic Cope <[email protected]>
  • Loading branch information
negz committed Oct 17, 2019
1 parent 2e3995c commit e7ecc64
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 266 deletions.
91 changes: 25 additions & 66 deletions cmd/angryjet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,23 @@ const (
CoreAlias = "corev1"
CoreImport = "k8s.io/api/core/v1"

RuntimeAlias = "runtimev1alpha1"
RuntimeImport = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1"
MetaAlias = "metav1"
MetaImport = "k8s.io/apimachinery/pkg/apis/meta/v1"

ResourceImport = "github.com/crossplaneio/crossplane-runtime/pkg/resource"
RuntimeAlias = "v1alpha1"
RuntimeImport = "github.com/crossplaneio/crossplane-runtime/apis/core/v1alpha1"
)

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:]))

Expand All @@ -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 non portable class method set for package %s", p.PkgPath)
}
}

Expand All @@ -99,10 +96,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),
}
Expand Down Expand Up @@ -130,18 +127,21 @@ func GenerateClaim(filename, header string, p *packages.Package) error {
"SetConditions": method.NewSetConditions(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(
Expand All @@ -153,49 +153,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{
Expand All @@ -207,7 +166,7 @@ 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")),
),
)
Expand Down
61 changes: 26 additions & 35 deletions internal/fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
36 changes: 4 additions & 32 deletions internal/match/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
)),
)
}
Expand All @@ -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.
Expand Down
Loading

0 comments on commit e7ecc64

Please sign in to comment.