-
Notifications
You must be signed in to change notification settings - Fork 63
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
Custom annotations for bundle-specific JSON schema fields #1957
Changes from 1 commit
9b588c0
12c4373
049e11b
8016d41
0221c28
a3fdc8d
79a88e9
c3d049f
b076813
073aeca
a6c45d5
d164968
00164a8
a579b1c
e15107f
9a55037
16042b5
0171513
d2bfb67
8f59695
40505b8
d3b30f7
43c4b58
aed0e0a
5194889
8a33fb3
6bc9664
1b31c09
7fb1ed8
9f3ec3d
3231cff
c619a73
3049184
0037671
7d8ae48
34d7484
386c7d3
835fb05
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,109 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/ghodss/yaml" | ||
|
||
"github.com/databricks/cli/libs/jsonschema" | ||
) | ||
|
||
type annotation struct { | ||
Description string `json:"description,omitempty"` | ||
MarkdownDescription string `json:"markdown_description,omitempty"` | ||
Title string `json:"title,omitempty"` | ||
Default any `json:"default,omitempty"` | ||
} | ||
|
||
type annotationHandler struct { | ||
pietern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
filePath string | ||
op *openapiParser | ||
ref map[string]annotation | ||
} | ||
|
||
const Placeholder = "PLACEHOLDER" | ||
|
||
// Adds annotations to the JSON schema reading from the annotations file. | ||
// More details https://json-schema.org/understanding-json-schema/reference/annotations | ||
func newAnnotationHandler(path string, op *openapiParser) (*annotationHandler, error) { | ||
b, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
data := map[string]annotation{} | ||
err = yaml.Unmarshal(b, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if data == nil { | ||
data = map[string]annotation{} | ||
} | ||
|
||
d := &annotationHandler{} | ||
d.ref = data | ||
d.op = op | ||
d.filePath = path | ||
return d, nil | ||
} | ||
|
||
func (d *annotationHandler) addAnnotations(typ reflect.Type, s jsonschema.Schema) jsonschema.Schema { | ||
// Skips the type if it is a part of OpenAPI spec, as it is already annotated. | ||
_, ok := d.op.findRef(typ) | ||
if ok { | ||
return s | ||
} | ||
|
||
refPath := jsonschema.TypePath(typ) | ||
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. I'd rather keep We can instead directly use |
||
items := map[string]*jsonschema.Schema{} | ||
items[refPath] = &s | ||
|
||
for k, v := range s.Properties { | ||
itemRefPath := fmt.Sprintf("%s.%s", refPath, k) | ||
items[itemRefPath] = v | ||
} | ||
|
||
for k, v := range items { | ||
// Skipping all default types like int, string, etc. | ||
shouldHandle := strings.HasPrefix(refPath, "github.com") | ||
if !shouldHandle { | ||
continue | ||
} | ||
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. The value of the map is a value type, so The |
||
|
||
item, ok := d.ref[k] | ||
if !ok { | ||
d.ref[k] = annotation{ | ||
Description: Placeholder, | ||
} | ||
continue | ||
} | ||
if item.Description == Placeholder { | ||
continue | ||
} | ||
|
||
v.Description = item.Description | ||
if item.Default != nil { | ||
v.Default = item.Default | ||
} | ||
v.MarkdownDescription = item.MarkdownDescription | ||
v.Title = item.Title | ||
} | ||
return s | ||
} | ||
|
||
func (d *annotationHandler) overwrite() error { | ||
data, err := yaml.Marshal(d.ref) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = os.WriteFile(d.filePath, data, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
return 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.
You can use
yamlsaver
for saving to disk.This gives you some control over the node style if you need it (e.g. use blocks for all descriptions).
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.
Updated to
yamlsaver
withyaml.LiteralStyle
to make editing easier, this style seems the best for this purpose