Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
Signed-off-by: MUzairS15 <[email protected]>
  • Loading branch information
MUzairS15 committed Jul 7, 2023
1 parent 0915d50 commit 9cc0f3c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
11 changes: 8 additions & 3 deletions utils/component/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package component
import "github.com/layer5io/meshkit/errors"

const (
ErrCrdGenerateCode = "11088"
ErrDefinitionCode = "11090"
ErrGetSchemaCode = "11091"
ErrCrdGenerateCode = "11088"
ErrDefinitionCode = "11090"
ErrGetSchemaCode = "11091"
ErrUpdateSchemaCode = "11092"
)

func ErrCrdGenerate(err error) error {
Expand All @@ -19,3 +20,7 @@ func ErrGetDefinition(err error) error {
func ErrGetSchema(err error) error {
return errors.New(ErrGetSchemaCode, errors.Alert, []string{"Could not get schema for the given CRD"}, []string{err.Error()}, []string{""}, []string{"Make sure that the given CRD is valid"})
}

func ErrUpdateSchema(err error, obj string) error {
return errors.New(ErrUpdateSchemaCode, errors.Alert, []string{"Could not update schema for the given CRD ", obj}, []string{err.Error()}, []string{}, []string{"Make sure that the given CRD is valid"})
}
38 changes: 25 additions & 13 deletions utils/component/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package component

import (
"encoding/json"
"errors"
"fmt"

"cuelang.org/go/cue"
Expand Down Expand Up @@ -92,25 +93,25 @@ func Generate(crd string) (v1alpha1.ComponentDefinition, error) {
}

/*
We walk the entire schema, looking for specfic peroperties that requires modification and store their path.
After the walk is complete, we iterate all paths and do the modification.
If any error occurs while updating schema properties, we return nil and skip the update.
We walk the entire schema, looking for specific peroperties that requires modification and store their path.
After the walk is complete, we iterate all paths and do the modification.
If any error occurs while updating schema properties, we return nil and skip the update.
*/
func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[string]interface{} {
func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) (map[string]interface{}, error) {
rootPath := fieldVal.Path().Selectors()

compProperties := fieldVal.LookupPath(cuePath)
crd, err := fieldVal.MarshalJSON()
if err != nil {
return nil
return nil, ErrUpdateSchema(err, group)
}

modified := make(map[string]interface{})
pathSelectors := [][]cue.Selector{}

err = json.Unmarshal(crd, &modified)
if err != nil {
return nil
return nil, ErrUpdateSchema(err, group)
}

compProperties.Walk(func(c cue.Value) bool {
Expand All @@ -124,7 +125,7 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st
}
})

/*
/*
"pathSelectors" contains all the paths from root to the property which needs to be modified.
*/
for _, selectors := range pathSelectors {
Expand All @@ -136,28 +137,39 @@ func UpdateProperties(fieldVal cue.Value, cuePath cue.Path, group string) map[st
selector := selectors[index]
selectorType := selector.Type()
s := selector.String()

if selectorType == cue.IndexLabel {
t := m.([]interface{})
t, ok := m.([]interface{})
if !ok {
return nil, ErrUpdateSchema(errors.New("error converting to []interface{}"), group)
}
token := selector.Index()
m = t[token].(map[string]interface{})
m, ok = t[token].(map[string]interface{})
if !ok {
return nil, ErrUpdateSchema(errors.New("error converting to map[string]interface{}"), group)
}
} else {
if selectorType == cue.StringLabel {
s = selector.Unquoted()
}
t := m.(map[string]interface{})
t, ok := m.(map[string]interface{})
if !ok {
return nil, ErrUpdateSchema(errors.New("error converting to map[string]interface{}"), group)
}
m = t[s]
}
index++
}

t := m.(map[string]interface{})
t, ok := m.(map[string]interface{})
if !ok {
return nil, ErrUpdateSchema(errors.New("error converting to map[string]interface{}"), group)
}
delete(t, "x-kubernetes-preserve-unknown-fields")
if m == nil {
m = make(map[string]interface{})
}
t["type"] = "string"
t["format"] = "textarea"
}
return modified
return modified, nil
}
11 changes: 6 additions & 5 deletions utils/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ func getSchema(parsedCrd cue.Value, pathConf CuePathConfig) (string, error) {
return "", ErrGetSchema(err)
}

updatedProps := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId)
// In case of any error while updating properties, nil is returned. Hence use the original schema.

if updatedProps != nil {
schema = updatedProps
updatedProps, err := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId)

if err != nil {
return "", err
}

schema = updatedProps

(schema)["title"] = manifests.FormatToReadableString(resourceId)
var output []byte
output, err = json.MarshalIndent(schema, "", " ")
Expand Down

0 comments on commit 9cc0f3c

Please sign in to comment.