Last active
August 26, 2023 07:45
-
-
Save KaitaoQiu/76bafa8a8cfa1233a7255941dcfdddc6 to your computer and use it in GitHub Desktop.
from cue to openapi, cue sample is from kubevela
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "encoding/json" | |
| "fmt" | |
| "log" | |
| "cuelang.org/go/cue" | |
| "cuelang.org/go/cue/load" | |
| "cuelang.org/go/encoding/openapi" | |
| ) | |
| func generateOpenAPI(defFile string, config *load.Config) ([]byte, error) { | |
| buildInstances := load.Instances([]string{defFile}, config) | |
| insts := cue.Build(buildInstances) | |
| b, err := openapi.Gen(insts[0], nil) | |
| if err != nil { | |
| return nil, err | |
| } | |
| var out bytes.Buffer | |
| err = json.Indent(&out, b, "", " ") | |
| if err != nil { | |
| return nil, err | |
| } | |
| return out.Bytes(), nil | |
| } | |
| func ExampleGenerateOpenAPI() { | |
| // Sample from kubevela cue tutorial | |
| // src := bytes.NewBufferString(` | |
| // import ("strings") | |
| // #parameter: { | |
| // outputs: [{ip: "1.1.1.1", hostname: "xxx.com"}, {ip: "2.2.2.2", hostname: "yyy.com"}] | |
| // } | |
| // #output: { | |
| // spec: { | |
| // if len(#parameter.outputs) > 0 { | |
| // _x: [ for _, v in #parameter.outputs { | |
| // "\(v.ip) \(v.hostname)" | |
| // }] | |
| // message: "Visiting URL: " + strings.Join(_x, "") | |
| // } | |
| // } | |
| // } | |
| // `) | |
| // Sample from https://github.com/kubevela/kubevela/issues/5365 | |
| src := bytes.NewBufferString(` | |
| #parameter: { | |
| // +usage=Specify the name of init container | |
| name: string | |
| // +usage=Specify the image of init container | |
| image: string | |
| // +usage=Specify image pull policy for your service | |
| imagePullPolicy: *"IfNotPresent" | "Always" | "Never" | |
| // +usage=Specify the commands run in the init container | |
| cmd?: [...string] | |
| // +usage=Specify the args run in the init container | |
| args?: [...string] | |
| // +usage=Specify the extra volume mounts for the init container | |
| extraVolumeMounts: [...{ | |
| // +usage=The name of the volume to be mounted | |
| name: string | |
| // +usage=The mountPath for mount in the init container | |
| mountPath: string | |
| }] | |
| }`) | |
| b, err := generateOpenAPI("-", &load.Config{ | |
| Stdin: src, | |
| }) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| // This contains the OpenAPI.json definition | |
| fmt.Println(string(b)) | |
| } | |
| func main() { | |
| ExampleGenerateOpenAPI() | |
| } | |
| // OUTPUT: | |
| // { | |
| // "openapi": "3.0.0", | |
| // "info": { | |
| // "title": "Generated by cue.", | |
| // "version": "no version" | |
| // }, | |
| // "paths": {}, | |
| // "components": { | |
| // "schemas": { | |
| // "parameter": { | |
| // "type": "object", | |
| // "required": [ | |
| // "name", | |
| // "image", | |
| // "imagePullPolicy", | |
| // "extraVolumeMounts" | |
| // ], | |
| // "properties": { | |
| // "name": { | |
| // "description": "+usage=Specify the name of init container", | |
| // "type": "string" | |
| // }, | |
| // "image": { | |
| // "description": "+usage=Specify the image of init container", | |
| // "type": "string" | |
| // }, | |
| // "imagePullPolicy": { | |
| // "description": "+usage=Specify image pull policy for your service", | |
| // "type": "string", | |
| // "enum": [ | |
| // "IfNotPresent", | |
| // "Always", | |
| // "Never" | |
| // ], | |
| // "default": "IfNotPresent" | |
| // }, | |
| // "cmd": { | |
| // "description": "+usage=Specify the commands run in the init container", | |
| // "type": "array", | |
| // "items": { | |
| // "type": "string" | |
| // } | |
| // }, | |
| // "args": { | |
| // "description": "+usage=Specify the args run in the init container", | |
| // "type": "array", | |
| // "items": { | |
| // "type": "string" | |
| // } | |
| // }, | |
| // "extraVolumeMounts": { | |
| // "description": "+usage=Specify the extra volume mounts for the init container", | |
| // "type": "array", | |
| // "items": { | |
| // "type": "object", | |
| // "required": [ | |
| // "name", | |
| // "mountPath" | |
| // ], | |
| // "properties": { | |
| // "name": { | |
| // "description": "+usage=The name of the volume to be mounted", | |
| // "type": "string" | |
| // }, | |
| // "mountPath": { | |
| // "description": "+usage=The mountPath for mount in the init container", | |
| // "type": "string" | |
| // } | |
| // } | |
| // } | |
| // } | |
| // } | |
| // } | |
| // } | |
| // } | |
| // } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment