From 2caa1affaf2fa0560ae7a8e8d2e8026beea0c691 Mon Sep 17 00:00:00 2001 From: Zhenhua Li Date: Mon, 4 Mar 2024 15:51:18 -0800 Subject: [PATCH] Sort resources and set in product in go compiler --- mmv1/api/product.go | 10 +++++++++- mmv1/api/resource.go | 23 +++++++++++++++++++++-- mmv1/api/type.go | 5 ++--- mmv1/main.go | 25 ++++++++++++++++--------- mmv1/provider/terraform.go | 2 +- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/mmv1/api/product.go b/mmv1/api/product.go index cf3bd9d33a48..d56edeefb9be 100644 --- a/mmv1/api/product.go +++ b/mmv1/api/product.go @@ -39,7 +39,7 @@ type Product struct { // Display Name: The full name of the GCP product; eg "Cloud Bigtable" - Objects []interface{} + Objects []*Resource // The list of permission scopes available for the service // For example: `https://www.googleapis.com/auth/compute` @@ -69,6 +69,14 @@ type Product struct { ClientName string `yaml:"client_name"` } +func (p *Product) Validate() { + // TODO Q1 Rewrite super + // super + for _, o := range p.Objects { + o.ProductMetadata = p + } +} + // def validate // super // set_variables @objects, :__product diff --git a/mmv1/api/resource.go b/mmv1/api/resource.go index 75557aaee73b..9fbb4d2b27d0 100644 --- a/mmv1/api/resource.go +++ b/mmv1/api/resource.go @@ -267,9 +267,28 @@ type Resource struct { // Add a deprecation message for a resource that's been deprecated in the API. DeprecationMessage string `yaml:"deprecation_message"` - Properties []Type + Properties []*Type - Parameters []Type + Parameters []*Type + + ProductMetadata *Product } // TODO: rewrite functions +func (r *Resource) Validate() { + // TODO Q1 Rewrite super + // super + + r.setResource(r.Parameters) + r.setResource(r.Properties) +} + +func (r *Resource) setResource(properties []*Type) { + if properties == nil { + return + } + + for _, property := range properties { + property.ResourceMetadata = r + } +} diff --git a/mmv1/api/type.go b/mmv1/api/type.go index b65547f1c9f3..0fe6f165e6dd 100644 --- a/mmv1/api/type.go +++ b/mmv1/api/type.go @@ -231,10 +231,9 @@ type Type struct { // just as they are in the standard flattener template. CustomFlatten string `yaml:"custom_flatten"` - __resource Resource + ResourceMetadata *Resource - // TODO: set a specific type intead of interface{} - __parent interface{} // is nil for top-level properties + ParentMetadata *Type // is nil for top-level properties } const MAX_NAME = 20 diff --git a/mmv1/main.go b/mmv1/main.go index b23108ac117a..b01aad864eaf 100644 --- a/mmv1/main.go +++ b/mmv1/main.go @@ -80,17 +80,17 @@ func main() { // TODO Q1: remove these lines, which are for debugging // log.Printf("productYamlPath %#v", productYamlPath) - var resources []api.Resource + var resources []*api.Resource = make([]*api.Resource, 0) productYaml, err := os.ReadFile(productYamlPath) if err != nil { log.Fatalf("Cannot open the file: %v", productYaml) } - productApi := api.Product{} - yamlValidator.Parse(productYaml, &productApi) + productApi := &api.Product{} + yamlValidator.Parse(productYaml, productApi) // TODO Q1: remove these lines, which are for debugging - // prod, _ := json.Marshal(&productApi) + // prod, _ := json.Marshal(productApi) // log.Printf("prod %s", string(prod)) if !productApi.ExistsAtVersionOrLower(version) { @@ -118,21 +118,29 @@ func main() { if err != nil { log.Fatalf("Cannot open the file: %v", resourceYamlPath) } - resource := api.Resource{} - yamlValidator.Parse(resourceYaml, &resource) + resource := &api.Resource{} + yamlValidator.Parse(resourceYaml, resource) // TODO Q1: remove these lines, which are for debugging - // res, _ := json.Marshal(&resource) + // res, _ := json.Marshal(resource) // log.Printf("resource %s", string(res)) // TODO Q1: add labels related fields + resource.Validate() resources = append(resources, resource) } // TODO Q2: override resources + log.Printf("resources before sorting %#v", resources) - // TODO Q1: sort resources by name and set in product + // Sort resources by name + sort.Slice(resources, func(i, j int) bool { + return resources[i].Name < resources[j].Name + }) + + productApi.Objects = resources + productApi.Validate() // TODO Q2: set other providers via flag providerToGenerate := provider.NewTerraform(productApi) @@ -142,7 +150,6 @@ func main() { continue } - // TODO Q1: generate templates log.Printf("%s: Generating files", productName) providerToGenerate.Generate(outputPath, productName, generateCode, generateDocs) } diff --git a/mmv1/provider/terraform.go b/mmv1/provider/terraform.go index 154e89c12736..fb620c1ddce5 100644 --- a/mmv1/provider/terraform.go +++ b/mmv1/provider/terraform.go @@ -34,7 +34,7 @@ type Terraform struct { ResourcesForVersion []api.Resource } -func NewTerraform(product api.Product) *Terraform { +func NewTerraform(product *api.Product) *Terraform { t := Terraform{ResourceCount: 0, IAMResourceCount: 0} // TODO Q1