Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Tweaks to field rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Porges committed Nov 23, 2020
1 parent a352aa7 commit 7d39558
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 6 deletions.
6 changes: 6 additions & 0 deletions hack/generator/pkg/astmodel/object_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ func (objectType *ObjectType) AsType(codeGenerationContext *CodeGenerationContex
fields = append(fields, f.AsField(codeGenerationContext))
}

if len(fields) > 0 {
// if first field has Before:EmptyLine decoration, switch it to NewLine
// this makes the output look nicer 🙂
fields[0].Decs.Before = ast.NewLine
}

return &ast.StructType{
Fields: &ast.FieldList{
List: fields,
Expand Down
7 changes: 6 additions & 1 deletion hack/generator/pkg/astmodel/property_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,17 @@ func (property *PropertyDefinition) AsField(codeGenerationContext *CodeGeneratio
AddValidationComments(&doc, validated.Validations().ToKubeBuilderValidations())
}

before := ast.NewLine
if len(doc) > 0 {
before = ast.EmptyLine
}

// We don't use StringLiteral() for the tag as it adds extra quotes
result := &ast.Field{
Decs: ast.FieldDecorations{
NodeDecs: ast.NodeDecs{
Before: ast.NewLine,
Start: doc,
Before: before,
},
},
Names: names,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Test struct {
// +kubebuilder:validation:Required
Enabled bool `json:"enabled"`
Name *string `json:"name,omitempty"`

// +kubebuilder:validation:Required
Size int `json:"size"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ type AList struct {
type A_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion ASpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type ASpecType `json:"type"`
}
Expand Down Expand Up @@ -121,8 +123,10 @@ type BList struct {
type B_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion BSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type BSpecType `json:"type"`
}
Expand Down Expand Up @@ -188,8 +192,10 @@ type CList struct {
type C_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion CSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type CSpecType `json:"type"`
}
Expand Down Expand Up @@ -228,6 +234,7 @@ type A_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down Expand Up @@ -283,6 +290,7 @@ type B_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"test.infra.azure.com" json:"owner" kind:"A"`
}
Expand Down Expand Up @@ -338,6 +346,7 @@ type C_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"test.infra.azure.com" json:"owner" kind:"B"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ type FakeResourceList struct {
type FakeResource_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
ArrayFoo []FooArm `json:"arrayFoo"`
ArrayOfArrays [][]FooArm `json:"arrayOfArrays,omitempty"`
ArrayOfEnums []Color `json:"arrayOfEnums,omitempty"`
ArrayOfMaps []map[string]FooArm `json:"arrayOfMaps,omitempty"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type FakeResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -105,6 +108,7 @@ const FakeResourceSpecTypeMicrosoftAzureFakeResource = FakeResourceSpecType("Mic
type FakeResource_Spec struct {
// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
ArrayFoo []Foo `json:"arrayFoo"`
ArrayOfArrays [][]Foo `json:"arrayOfArrays,omitempty"`
Expand All @@ -114,6 +118,7 @@ type FakeResource_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ type FakeResource_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`
Color *FakeResourceSpecColor `json:"color,omitempty"`

// +kubebuilder:validation:Required
Foo FooArm `json:"foo"`

// +kubebuilder:validation:Required
Name string `json:"name"`
OptionalFoo *FooArm `json:"optionalFoo,omitempty"`

// +kubebuilder:validation:Required
Type FakeResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -108,9 +111,11 @@ type FakeResource_Spec struct {
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`
Color *FakeResourceSpecColor `json:"color,omitempty"`

// +kubebuilder:validation:Required
Foo Foo `json:"foo"`
OptionalFoo *Foo `json:"optionalFoo,omitempty"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ type FakeResourceList struct {
type FakeResource_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
JsonObject map[string]v1.JSON `json:"jsonObject"`

// +kubebuilder:validation:Required
MandatoryJson v1.JSON `json:"mandatoryJson"`

// +kubebuilder:validation:Required
Name string `json:"name"`
OptionalJson *v1.JSON `json:"optionalJson,omitempty"`

// +kubebuilder:validation:Required
Type FakeResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -100,11 +104,14 @@ type FakeResource_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`

// +kubebuilder:validation:Required
JsonObject map[string]v1.JSON `json:"jsonObject"`

// +kubebuilder:validation:Required
MandatoryJson v1.JSON `json:"mandatoryJson"`
OptionalJson *v1.JSON `json:"optionalJson,omitempty"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ type FakeResourceList struct {
type FakeResource_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
MapFoo map[string]FooArm `json:"mapFoo"`
MapOfArrays map[string][]FooArm `json:"mapOfArrays,omitempty"`
MapOfEnums map[string]Color `json:"mapOfEnums,omitempty"`
MapOfMaps map[string]map[string]FooArm `json:"mapOfMaps,omitempty"`
MapOfStrings map[string]string `json:"mapOfStrings,omitempty"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type FakeResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -110,12 +113,14 @@ type FakeResource_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`

// +kubebuilder:validation:Required
MapFoo map[string]Foo `json:"mapFoo"`
MapOfArrays map[string][]Foo `json:"mapOfArrays,omitempty"`
MapOfEnums map[string]Color `json:"mapOfEnums,omitempty"`
MapOfMaps map[string]map[string]Foo `json:"mapOfMaps,omitempty"`
MapOfStrings map[string]string `json:"mapOfStrings,omitempty"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ type FakeResourceList struct {
type FakeResource_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type FakeResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -94,6 +96,7 @@ type FakeResource_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ type FakeResourceList struct {

type FakeResource_SpecArm struct {
EmbeddedTestType `json:",inline"`

// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`
Color *FakeResourceSpecColor `json:"color,omitempty"`

// +kubebuilder:validation:Required
Foo FooArm `json:"foo"`

// +kubebuilder:validation:Required
Name string `json:"name"`
OptionalFoo *FooArm `json:"optionalFoo,omitempty"`

// +kubebuilder:validation:Required
Type FakeResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -103,16 +107,19 @@ const FakeResourceSpecTypeMicrosoftAzureFakeResource = FakeResourceSpecType("Mic

type FakeResource_Spec struct {
EmbeddedTestType `json:",inline"`

// +kubebuilder:validation:Required
ApiVersion FakeResourceSpecApiVersion `json:"apiVersion"`

//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName string `json:"azureName"`
Color *FakeResourceSpecColor `json:"color,omitempty"`

// +kubebuilder:validation:Required
Foo Foo `json:"foo"`
OptionalFoo *Foo `json:"optionalFoo,omitempty"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ type AResourceList struct {
type AResource_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion AResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type AResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -94,6 +96,7 @@ type AResource_Spec struct {
//AzureName: The name of the resource in Azure. This is often the same as the name
//of the resource in Kubernetes but it doesn't have to be.
AzureName AResourceSpecName `json:"azureName"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ const AResourceSpecNameOnlyonevalue = AResourceSpecName("onlyonevalue")
type AResource_SpecArm struct {
// +kubebuilder:validation:Required
ApiVersion AResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
Name string `json:"name"`

// +kubebuilder:validation:Required
Type AResourceSpecType `json:"type"`
}
Expand Down Expand Up @@ -83,6 +85,7 @@ const AResourceSpecTypeMicrosoftAzureAResource = AResourceSpecType("Microsoft.Az
type AResource_Spec struct {
// +kubebuilder:validation:Required
ApiVersion AResourceSpecApiVersion `json:"apiVersion"`

// +kubebuilder:validation:Required
Owner genruntime.KnownResourceReference `group:"microsoft.resources.infra.azure.com" json:"owner" kind:"ResourceGroup"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type Test struct {
}

type Test_Properties struct {

//Bar: Mutually exclusive with all other properties
Bar *Bar `json:"bar,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "encoding/json"

//Generated from: https://test.test/schemas/2020-01-01/test.json
type Test struct {

//Bar: Mutually exclusive with all other properties
Bar *Bar `json:"bar,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "encoding/json"

//Generated from: https://test.test/schemas/2020-01-01/test.json
type Test struct {

//Bool1: Mutually exclusive with all other properties
Bool1 *bool `json:"bool1,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "encoding/json"

//Generated from: https://test.test/schemas/2020-01-01/test.json
type Test struct {

//Either: Mutually exclusive with all other properties
Either *Test_Either `json:"either,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "encoding/json"

//Generated from: https://test.test/schemas/2020-01-01/test.json
type Test struct {

//Base: Mutually exclusive with all other properties
Base *Test_Base `json:"base,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ type Test struct {
// +kubebuilder:validation:MinItems=1
// +kubebuilder:validation:UniqueItems=true
Apple []float64 `json:"apple"`

// +kubebuilder:validation:Required
// +kubebuilder:validation:MaxLength=20
// +kubebuilder:validation:MinLength=10
// +kubebuilder:validation:Pattern="^[a-z]+$"
Duck string `json:"duck"`

// +kubebuilder:validation:Required
// +kubebuilder:validation:Maximum=2000
// +kubebuilder:validation:Minimum=1000
// +kubebuilder:validation:ExclusiveMinimum=true
// +kubebuilder:validation:MultipleOf=3
Moon int `json:"moon"`

// +kubebuilder:validation:Required
// +kubebuilder:validation:Maximum=200
// +kubebuilder:validation:ExclusiveMaximum=true
Expand Down

0 comments on commit 7d39558

Please sign in to comment.