-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support generation of model from openapi schemas #582
Changes from 3 commits
bdfb934
996d126
099cdd3
66434f0
368373e
c859ff0
5ba263e
b8eebd9
e757428
112a538
ad5c265
45521cd
98a1e99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package encoding | ||
|
||
import ( | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func ToYaml(data []byte) ([]byte, error) { | ||
var out map[string]interface{} | ||
err := Unmarshal(data, &out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return yaml.Marshal(out) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package component | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"cuelang.org/go/cue" | ||
"cuelang.org/go/cue/cuecontext" | ||
"cuelang.org/go/encoding/yaml" | ||
"github.com/layer5io/meshkit/utils/manifests" | ||
"github.com/meshery/schemas/models/v1beta1" | ||
"github.com/meshery/schemas/models/v1beta1/category" | ||
"github.com/meshery/schemas/models/v1beta1/component" | ||
"github.com/meshery/schemas/models/v1beta1/model" | ||
) | ||
|
||
func GenerateFromOpenAPI(resource string) ([]component.ComponentDefinition, error) { | ||
cuectx := cuecontext.New() | ||
fmt.Println("resource -----------: ", resource) | ||
cueParsedManExpr, err := yaml.Extract("", []byte(resource)) | ||
parsedManifest := cuectx.BuildFile(cueParsedManExpr) | ||
|
||
definitions := parsedManifest.LookupPath(cue.ParsePath("components.schemas")) | ||
if err != nil { | ||
return nil, nil | ||
} | ||
|
||
resol := manifests.ResolveOpenApiRefs{} | ||
cache := make(map[string][]byte) | ||
resolved, err := resol.ResolveReferences([]byte(resource), definitions, cache) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fmt.Println("resource -----------: ", string(resolved)) | ||
cueParsedManExpr, err = yaml.Extract("", []byte(resolved)) | ||
parsedManifest = cuectx.BuildFile(cueParsedManExpr) | ||
definitions = parsedManifest.LookupPath(cue.ParsePath("components.schemas")) | ||
if err != nil { | ||
return nil, nil | ||
} | ||
|
||
fields, err := definitions.Fields() | ||
if err != nil { | ||
fmt.Printf("%v\n", err) | ||
return nil, err | ||
} | ||
components := make([]component.ComponentDefinition, 0) | ||
|
||
for fields.Next() { | ||
fieldVal := fields.Value() | ||
kindCue := fieldVal.LookupPath(cue.ParsePath(`"x-kubernetes-group-version-kind"[0].kind`)) | ||
if kindCue.Err() != nil { | ||
continue | ||
} | ||
kind, err := kindCue.String() | ||
kind = strings.ToLower(kind) | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
|
||
crd, err := fieldVal.MarshalJSON() | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
versionCue := fieldVal.LookupPath(cue.ParsePath(`"x-kubernetes-group-version-kind"[0].version`)) | ||
groupCue := fieldVal.LookupPath(cue.ParsePath(`"x-kubernetes-group-version-kind"[0].group`)) | ||
apiVersion, _ := versionCue.String() | ||
if g, _ := groupCue.String(); g != "" { | ||
apiVersion = g + "/" + apiVersion | ||
} | ||
modified := make(map[string]interface{}) //Remove the given fields which is either not required by End user (like status) or is prefilled by system (like apiVersion, kind and metadata) | ||
err = json.Unmarshal(crd, &modified) | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
|
||
modifiedProps, err := UpdateProperties(fieldVal, cue.ParsePath("properties.spec"), apiVersion) | ||
if err == nil { | ||
modified = modifiedProps | ||
} | ||
|
||
DeleteFields(modified) | ||
crd, err = json.Marshal(modified) | ||
if err != nil { | ||
fmt.Printf("%v", err) | ||
continue | ||
} | ||
|
||
c := component.ComponentDefinition{ | ||
SchemaVersion: v1beta1.ComponentSchemaVersion, | ||
Version: "v1.0.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding appropriate? |
||
|
||
Format: component.JSON, | ||
Component: component.Component{ | ||
Kind: kind, | ||
Version: apiVersion, | ||
Schema: string(crd), | ||
}, | ||
// Metadata: compMetadata, | ||
DisplayName: manifests.FormatToReadableString(kind), | ||
Model: model.ModelDefinition{ | ||
SchemaVersion: v1beta1.ModelSchemaVersion, | ||
Version: "v1.0.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this still hardcoded? |
||
|
||
Model: model.Model{ | ||
Version: "version", | ||
}, | ||
Name: "kubernetes", | ||
DisplayName: "Kubernetes", | ||
Category: category.CategoryDefinition{ | ||
Name: "Orchestration & Management", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update |
||
}, | ||
}, | ||
} | ||
components = append(components, c) | ||
} | ||
return components, nil | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass the "pkg" that contains info regarding model to generate.