Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions mmv1/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Loader struct {
// baseDirectory points to mmv1 root, if cwd can be empty as relative paths are used
baseDirectory string
overrideDirectory string
Products map[string]*api.Product
version string
sysfs google.ReadDirReadFileFS
}
Expand Down Expand Up @@ -53,7 +54,7 @@ func NewLoader(config Config) *Loader {
return l
}

func (l *Loader) LoadProducts() map[string]*api.Product {
func (l *Loader) LoadProducts() {
if l.version == "" {
log.Printf("No version specified, assuming ga")
l.version = "ga"
Expand Down Expand Up @@ -90,7 +91,7 @@ func (l *Loader) LoadProducts() map[string]*api.Product {
}
}

return l.batchLoadProducts(allProductFiles)
l.Products = l.batchLoadProducts(allProductFiles)
}

func (l *Loader) batchLoadProducts(productNames []string) map[string]*api.Product {
Expand Down Expand Up @@ -306,10 +307,6 @@ func (l *Loader) loadResource(product *api.Product, baseResourcePath string, ove
resource.TargetVersionName = l.version
// SetDefault before AddExtraFields to ensure relevant metadata is available on existing fields
resource.SetDefault(product)
resource.Properties = resource.AddExtraFields(resource.PropertiesWithExcluded(), nil)
// SetDefault after AddExtraFields to ensure relevant metadata is available for the newly generated fields
resource.SetDefault(product)
resource.Validate()
resource.TestSampleSetUp(l.sysfs)

for _, e := range resource.Examples {
Expand All @@ -320,3 +317,31 @@ func (l *Loader) loadResource(product *api.Product, baseResourcePath string, ove

return resource
}

func (l *Loader) AddExtraFields() error {
if l.Products == nil {
return errors.New("products have not been loaded into memory")
}

for _, product := range l.Products {
for _, resource := range product.Objects {
resource.Properties = resource.AddExtraFields(resource.PropertiesWithExcluded(), nil)
// SetDefault after AddExtraFields to ensure relevant metadata is available for the newly generated fields
resource.SetDefault(product)
}
}

return nil
}

func (l *Loader) Validate() {
if l.Products == nil {
log.Fatalln("products have not been loaded into memory")
}

for _, product := range l.Products {
for _, resource := range product.Objects {
resource.Validate()
}
}
}
5 changes: 4 additions & 1 deletion mmv1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func GenerateProducts(product, resource, providerName, version, outputPath, base
}

loader := loader.NewLoader(loader.Config{Version: version, BaseDirectory: baseDirectory, OverrideDirectory: overrideDirectory, Sysfs: ofs})
loadedProducts := loader.LoadProducts()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be missing the "why" of the change overall, but I had a slight preference for the well encapsulated, low coupling previous version of the code.

loadedProducts := loader.LoadProducts() lets the loader do everything behind the scenes and return the products.

Could you keep that? It seems a bit "complicated" for users of the Loader type to need to know that they need to call LoadProducts, AddExtraFields, and Validate in that order, then access .Products.

I think I preferred having all of those methods private implementation details, and only have one public LoadProducts().

The rest of the change LGTM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicdumz we need to be able to load the yaml files, modify them in-memory, and write them out again - which means we need to separate loading from any "post-processing" like AddExtraFields.

loader.LoadProducts()
loader.AddExtraFields()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicdumz pointed out this returns an error that should be handled.

loader.Validate()
loadedProducts := loader.Products

var productsToGenerate []string
if product == "" {
Expand Down
Loading