Skip to content

Commit

Permalink
Set default values for Go properties (#10317)
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 authored Mar 29, 2024
1 parent 54ef46d commit 9c251ef
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 32 deletions.
3 changes: 0 additions & 3 deletions mmv1/api/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ func (p *Product) UnmarshalYAML(n *yaml.Node) error {
func (p *Product) Validate() {
// TODO Q2 Rewrite super
// super
for _, o := range p.Objects {
o.ProductMetadata = p
}
}

// def validate
Expand Down
14 changes: 4 additions & 10 deletions mmv1/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,18 +306,12 @@ func (r *Resource) UnmarshalYAML(n *yaml.Node) error {
func (r *Resource) Validate() {
// TODO Q1 Rewrite super
// super

r.setResourceMetada(r.Parameters)
r.setResourceMetada(r.Properties)
}

func (r *Resource) setResourceMetada(properties []*Type) {
if properties == nil {
return
}

for _, property := range properties {
property.ResourceMetadata = r
func (r *Resource) SetDefault(product *Product) {
r.ProductMetadata = product
for _, property := range r.AllProperties() {
property.SetDefault(r)
}
}

Expand Down
47 changes: 46 additions & 1 deletion mmv1/api/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ type Type struct {
ItemType *Type `yaml:"item_type"`
MinSize int `yaml:"min_size"`
MaxSize int `yaml:"max_size"`
// __name
ParentName string

// ====================
// ResourceRef Fields
// ====================
Resource string
Imports string

Expand Down Expand Up @@ -270,7 +275,47 @@ type Type struct {

const MAX_NAME = 20

// func (t *Type) validate() {
func (t *Type) SetDefault(r *Resource) {
t.ResourceMetadata = r
if t.UpdateVerb == "" {
t.UpdateVerb = t.ResourceMetadata.UpdateVerb
}

switch {
case t.IsA("Array"):
t.ItemType.ParentName = t.Name
t.ItemType.ParentMetadata = t.ParentMetadata
t.ItemType.SetDefault(r)
case t.IsA("Map"):
t.KeyExpander = "tpgresource.ExpandString"
t.ValueType.ParentName = t.Name
t.ValueType.ParentMetadata = t.ParentMetadata
t.ValueType.SetDefault(r)
case t.IsA("NestedObject"):
if t.Name == "" {
t.Name = t.ParentName
}

if t.Description == "" {
t.Description = "A nested object resource"
}

for _, p := range t.Properties {
p.ParentMetadata = t
p.SetDefault(r)
}
case t.IsA("ResourceRef"):
if t.Name == "" {
t.Name = t.Resource
}

if t.Description == "" {
t.Description = fmt.Sprintf("A reference to %s resource", t.Resource)
}
default:
}
}

// super
// check :description, type: ::String, required: true
// check :exclude, type: :boolean, default: false, required: true
Expand Down
14 changes: 0 additions & 14 deletions mmv1/api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -864,20 +864,6 @@ def nested_properties
end
end

# Support for schema ValidateFunc functionality.
class Validation < Object
# Ensures the value matches this regex
attr_reader :regex
attr_reader :function

def validate
super

check :regex, type: String
check :function, type: String
end
end

def type?(type)
type.is_a?(Type) || !get_type(type).nil?
end
Expand Down
1 change: 0 additions & 1 deletion mmv1/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.21

require (
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
4 changes: 2 additions & 2 deletions mmv1/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68=
github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 1 addition & 1 deletion mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func main() {
api.Compile(resourceYamlPath, resource)

resource.Properties = resource.AddLabelsRelatedFields(resource.PropertiesWithExcluded(), nil)

resource.SetDefault(productApi)
resource.Validate()
resources = append(resources, resource)
}
Expand Down

0 comments on commit 9c251ef

Please sign in to comment.