-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate a JSON schema for the YAML dashboard model
- Loading branch information
Showing
8 changed files
with
1,768 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/K-Phoen/grabana/decoder" | ||
"github.com/invopop/jsonschema" | ||
) | ||
|
||
func main() { | ||
types := []struct { | ||
name string | ||
input any | ||
}{ | ||
{ | ||
name: "dashboard", | ||
input: &decoder.DashboardModel{}, | ||
}, | ||
} | ||
|
||
for _, t := range types { | ||
fmt.Printf("Generating schema for type '%s'\n", t.name) | ||
|
||
reflector := &jsonschema.Reflector{ | ||
RequiredFromJSONSchemaTags: true, // we don't have good information as to what is required :/ | ||
FieldNameTag: "yaml", | ||
KeyNamer: func(key string) string { | ||
if strings.ToUpper(string(key[0])) == string(key[0]) { | ||
return strings.ToLower(key) | ||
} | ||
|
||
return key | ||
}, | ||
} | ||
schema := reflector.Reflect(t.input) | ||
schema.ID = jsonschema.ID(fmt.Sprintf("https://raw.githubusercontent.com/K-Phoen/grabana/master/schemas/%s.json", t.name)) | ||
|
||
schemaJSON, err := json.MarshalIndent(schema, "", " ") | ||
if err != nil { | ||
panic(fmt.Errorf("could not marshal schema to JSON: %w", err)) | ||
} | ||
|
||
if err := os.WriteFile(fmt.Sprintf("./schemas/%s.json", t.name), schemaJSON, 0644); err != nil { | ||
Check failure on line 46 in cmd/generate-jsonschema/main.go GitHub Actions / Tests (1.20)
|
||
panic(fmt.Errorf("could not write schema: %w", err)) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.