Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 215
Make bundle JSON schema modular with $defs#1700
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
75f252e43325fd6d2f88265f1c757db32fcb899285b023ba0e24725d460eeb900e589608133127c7017939efb70790731f535f67011dfdc0870e4192dea889d07192ffcdccb3e7fd0635b79747ac601632c30cfa483480f727036bacc4309f194a5bad7503a40f4d35aac66874141f4ecb8d6a9578019b176ced13d5c076be0ad488575c4746585bb6bd7ad066cbb54ba3f0049d9c612446463bbfe9cc4a64857e42da1c9d0aa4934379d7fe55df7b1dd399f75a571a8cd2631c69e6d92d62c0c5f48b58ab3dd7a804d3794c3faccd9432e565ed301e1a8e03File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package main | ||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "reflect" | ||
| "github.com/databricks/cli/bundle/config" | ||
| "github.com/databricks/cli/bundle/config/variable" | ||
| "github.com/databricks/cli/libs/jsonschema" | ||
| ) | ||
| func interpolationPattern(s string) string { | ||
| return fmt.Sprintf(`\$\{(%s(\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\[[0-9]+\])*)+)\}`, s) | ||
| } | ||
| func addInterpolationPatterns(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema { | ||
| if typ == reflect.TypeOf(config.Root{}) || typ == reflect.TypeOf(variable.Variable{}) { | ||
| return s | ||
| } | ||
| switch s.Type { | ||
| case jsonschema.ArrayType, jsonschema.ObjectType: | ||
| // arrays and objects can have complex variable values specified. | ||
| return jsonschema.Schema{ | ||
| AnyOf: []jsonschema.Schema{ | ||
| s, | ||
| { | ||
| Type: jsonschema.StringType, | ||
| Pattern: interpolationPattern("var"), | ||
| }}, | ||
| } | ||
| case jsonschema.IntegerType, jsonschema.NumberType, jsonschema.BooleanType: | ||
| // primitives can have variable values, or references like ${bundle.xyz} | ||
| // or ${workspace.xyz} | ||
| return jsonschema.Schema{ | ||
| AnyOf: []jsonschema.Schema{ | ||
| s, | ||
| {Type: jsonschema.StringType, Pattern: interpolationPattern("resources")}, | ||
| {Type: jsonschema.StringType, Pattern: interpolationPattern("bundle")}, | ||
| {Type: jsonschema.StringType, Pattern: interpolationPattern("workspace")}, | ||
| {Type: jsonschema.StringType, Pattern: interpolationPattern("artifacts")}, | ||
| {Type: jsonschema.StringType, Pattern: interpolationPattern("var")}, | ||
| }, | ||
| } | ||
| default: | ||
| return s | ||
| } | ||
| } | ||
| func main() { | ||
| if len(os.Args) != 2 { | ||
| fmt.Println("Usage: go run main.go <output-file>") | ||
| os.Exit(1) | ||
| } | ||
| // Output file, where the generated JSON schema will be written to. | ||
| outputFile := os.Args[1] | ||
| // Input file, the databricks openapi spec. | ||
| inputFile := os.Getenv("DATABRICKS_OPENAPI_SPEC") | ||
| if inputFile == "" { | ||
| log.Fatal("DATABRICKS_OPENAPI_SPEC environment variable not set") | ||
| } | ||
| p, err := newParser(inputFile) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| // Generate the JSON schema from the bundle Go struct. | ||
| s, err := jsonschema.FromType(reflect.TypeOf(config.Root{}), []func(reflect.Type, jsonschema.Schema) jsonschema.Schema{ | ||
| p.addDescriptions, | ||
| p.addEnums, | ||
| addInterpolationPatterns, | ||
| }) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| b, err := json.MarshalIndent(s, "", " ") | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| // Write the schema descriptions to the output file. | ||
| err = os.WriteFile(outputFile, b, 0644) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package main | ||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
| "reflect" | ||
| "strings" | ||
| "github.com/databricks/cli/libs/jsonschema" | ||
| ) | ||
| type Components struct { | ||
| Schemas map[string]jsonschema.Schema `json:"schemas,omitempty"` | ||
| } | ||
| type Specification struct { | ||
| Components Components `json:"components"` | ||
| } | ||
| type openapiParser struct { | ||
| ref map[string]jsonschema.Schema | ||
| } | ||
| func newParser(path string) (*openapiParser, error) { | ||
| b, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| spec := Specification{} | ||
| err = json.Unmarshal(b, &spec) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| p := &openapiParser{} | ||
| p.ref = spec.Components.Schemas | ||
| return p, nil | ||
| } | ||
| // This function checks if the input type: | ||
| // 1. Is a Databricks Go SDK type. | ||
| // 2. Has a Databricks Go SDK type embedded in it. | ||
| // | ||
| // If the above conditions are met, the function returns the JSON schema | ||
| // corresponding to the Databricks Go SDK type from the OpenAPI spec. | ||
| func (p *openapiParser) findRef(typ reflect.Type) (jsonschema.Schema, bool) { | ||
| typs := []reflect.Type{typ} | ||
| // Check for embedded Databricks Go SDK types. | ||
| if typ.Kind() == reflect.Struct { | ||
| for i := 0; i < typ.NumField(); i++ { | ||
| if !typ.Field(i).Anonymous { | ||
| continue | ||
| } | ||
| // Deference current type if it's a pointer. | ||
| ctyp := typ.Field(i).Type | ||
| for ctyp.Kind() == reflect.Ptr { | ||
| ctyp = ctyp.Elem() | ||
| } | ||
| typs = append(typs, ctyp) | ||
| } | ||
| } | ||
| for _, ctyp := range typs { | ||
| // Skip if it's not a Go SDK type. | ||
| if !strings.HasPrefix(ctyp.PkgPath(), "github.com/databricks/databricks-sdk-go") { | ||
| continue | ||
| } | ||
| pkgName := path.Base(ctyp.PkgPath()) | ||
| k := fmt.Sprintf("%s.%s", pkgName, ctyp.Name()) | ||
| // Skip if the type is not in the openapi spec. | ||
| _, ok := p.ref[k] | ||
| if !ok { | ||
| continue | ||
| } | ||
| // Return the first Go SDK type found in the openapi spec. | ||
| return p.ref[k], true | ||
| } | ||
| return jsonschema.Schema{}, false | ||
| } | ||
| // Use the OpenAPI spec to load descriptions for the given type. | ||
| func (p *openapiParser) addDescriptions(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema { | ||
| ref, ok := p.findRef(typ) | ||
| if !ok { | ||
| return s | ||
| } | ||
| s.Description = ref.Description | ||
| for k, v := range s.Properties { | ||
| if refProp, ok := ref.Properties[k]; ok { | ||
| v.Description = refProp.Description | ||
| } | ||
| } | ||
| return s | ||
| } | ||
| // Use the OpenAPI spec add enum values for the given type. | ||
| func (p *openapiParser) addEnums(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema { | ||
| ref, ok := p.findRef(typ) | ||
| if !ok { | ||
| return s | ||
| } | ||
| s.Enum = append(s.Enum, ref.Enum...) | ||
| for k, v := range s.Properties { | ||
| if refProp, ok := ref.Properties[k]; ok { | ||
| v.Enum = append(v.Enum, refProp.Enum...) | ||
| } | ||
| } | ||
| return s | ||
| } | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.