Skip to content

Commit

Permalink
update spec path
Browse files Browse the repository at this point in the history
Signed-off-by: MUzairS15 <muzair.shaikh810@gmail.com>
  • Loading branch information
MUzairS15 committed Jul 19, 2023
1 parent e232aec commit 3e866e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions utils/component/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var DefaultPathConfig = CuePathConfig{
VersionPath: "spec.versions[0].name",
GroupPath: "spec.group",
ScopePath: "spec.scope",
SpecPath: "spec.versions[0].schema.openAPIV3Schema.properties.spec",
SpecPath: "spec.versions[0].schema.openAPIV3Schema",
PropertiesPath: "properties",
}

Expand All @@ -41,7 +41,7 @@ var DefaultPathConfig2 = CuePathConfig{
VersionPath: "spec.versions[0].name",
GroupPath: "spec.group",
ScopePath: "spec.scope",
SpecPath: "spec.validation.openAPIV3Schema.properties.spec",
SpecPath: "spec.validation.openAPIV3Schema",
}

var Configs = []CuePathConfig{DefaultPathConfig, DefaultPathConfig2}
Expand Down
23 changes: 21 additions & 2 deletions utils/component/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/layer5io/meshkit/utils/manifests"
)

// Remove the fields which is either not required by end user (like status) or is prefilled by system (like apiVersion, kind and metadata)
var fieldsToDelete = [4]string{"apiVersion", "kind", "status", "metadata"}

// extracts the JSONSCHEMA of the CRD and outputs the json encoded string of the schema
func getSchema(parsedCrd cue.Value, pathConf CuePathConfig) (string, error) {
schema := map[string]interface{}{}
Expand All @@ -27,14 +30,15 @@ func getSchema(parsedCrd cue.Value, pathConf CuePathConfig) (string, error) {
if err != nil {
return "", ErrGetSchema(err)
}

updatedProps, err := UpdateProperties(specCueVal, cue.ParsePath(pathConf.PropertiesPath), resourceId)

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

schema = updatedProps
DeleteFields(schema)

(schema)["title"] = manifests.FormatToReadableString(resourceId)
var output []byte
Expand All @@ -56,3 +60,18 @@ func extractCueValueFromPath(crd cue.Value, pathConf string) (string, error) {
}
return res, nil
}

// function to remove fields that are not required or prefilled
func DeleteFields(m map[string]interface{}) {
key := "properties"
if m[key] == nil {
return
}
if prop, ok := m[key].(map[string]interface{}); ok && prop != nil {
for _, f := range fieldsToDelete {
delete(prop, f)
}
m[key] = prop
}
}

0 comments on commit 3e866e3

Please sign in to comment.