This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Generate jsonnet from json #3
Draft
malcolmholmes
wants to merge
7
commits into
master
Choose a base branch
from
generate-jsonnet-from-json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e9dbe34
Put templates for building libraries into a dir
malcolmholmes 6ce0276
Fixes to specs
malcolmholmes 3049636
Templates for dashboard instances
malcolmholmes 8683980
Ignore binary
malcolmholmes 1989134
Support for instance generation as well as libraries
malcolmholmes 3b18b2c
Change CLI signature in drone
malcolmholmes b848b95
Add Grafana alerts to spec
malcolmholmes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
_gen | ||
dashboard-spec |
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,108 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/go-openapi/inflect" | ||
) | ||
|
||
func generateInstance(s Spec, l Language, instanceFile string) error { | ||
|
||
// Create directories. | ||
dir := path.Join("_gen", s.Info.Version, l.Directory) | ||
os.MkdirAll(dir, os.ModePerm) | ||
for _, d := range []string{"panel", "target", "template"} { | ||
os.MkdirAll(path.Join(dir, d), os.ModePerm) | ||
} | ||
|
||
// Function that renders templates and writes them to files. | ||
//g := func(name string, tmplType string, data interface{}) error { | ||
|
||
dashboardString, err := ioutil.ReadFile(instanceFile) | ||
if err != nil { | ||
return err | ||
} | ||
dashboard := map[string]interface{}{} | ||
err = json.Unmarshal(dashboardString, &dashboard) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outFile := strings.ReplaceAll(instanceFile, ".json", "") + ".libsonnet" | ||
|
||
f, err := os.Create(outFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
tmplFile := "dashboard.tmpl" | ||
tmpl, err := template.New(tmplFile).Funcs( | ||
template.FuncMap{ | ||
"objectInflection": l.OjectInflection, | ||
"singularize": inflect.Singularize, | ||
"add": func(x int, y int) int { | ||
return x + y | ||
}, | ||
"subtract": func(x int, y int) int { | ||
return x - y | ||
}, | ||
"repeat": func(s string, n int) string { | ||
return strings.Repeat(s, n) | ||
}, | ||
"toString": func(value interface{}, valueType, name string) (string, error) { | ||
switch valueType { | ||
case "string": | ||
if value == nil { | ||
return "", nil | ||
} | ||
fmt.Fprintf(os.Stderr, "%s: %v\n", name, value) | ||
return fmt.Sprintf("\"%v\"", value.(string)), nil | ||
case "integer": | ||
return fmt.Sprintf("%v", value), nil | ||
case "boolean": | ||
return fmt.Sprintf("%v", value), nil | ||
default: | ||
return "", nil //fmt.Errorf("Cannot convert %v (%s) to string", v, reflect.TypeOf(v)) | ||
} | ||
}, | ||
"toKey": func(s string) string { | ||
return inflect.CamelizeDownFirst(s) | ||
}, | ||
"toCamel": func(s string) string { | ||
return inflect.Camelize(s) | ||
}, | ||
Comment on lines
+74
to
+79
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. Can't we use |
||
"debug": func(message string, value interface{}) string { | ||
message = fmt.Sprintf("DEBUG: %s %v\n", message, value) | ||
fmt.Fprintf(os.Stderr, message) | ||
return message | ||
}, | ||
"debugx": func(message string, value interface{}) string { | ||
b, _ := json.MarshalIndent(value, " ", " ") | ||
message = fmt.Sprintf("DEBUG: %s %v\n", message, string(b)) | ||
fmt.Fprintf(os.Stderr, message) | ||
return "" | ||
}, // wrap overcomes Golang template's inability to pass in multiple arguments to a template | ||
"wrap": func(values ...interface{}) []interface{} { | ||
return values | ||
}, | ||
}, | ||
).ParseFiles( | ||
path.Join("templates", l.Directory, "instance", tmplFile), | ||
path.Join("templates", l.Directory, "instance", "_shared.tmpl"), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
data := map[string]interface{}{} | ||
data["spec"] = s.Components.Schemas | ||
data["instance"] = dashboard | ||
|
||
err = tmpl.Execute(f, data) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/go-openapi/inflect" | ||
) | ||
|
||
func generateLibrary(s Spec, l Language) error { | ||
|
||
// Create directories. | ||
dir := path.Join("_gen", s.Info.Version, l.Directory) | ||
os.MkdirAll(dir, os.ModePerm) | ||
for _, d := range []string{"panel", "target", "template"} { | ||
os.MkdirAll(path.Join(dir, d), os.ModePerm) | ||
} | ||
|
||
// Function that renders templates and writes them to files. | ||
g := func(name string, tmplType string, data interface{}) error { | ||
tmplFile := fmt.Sprintf("%s.tmpl", tmplType) | ||
tmpl, err := template.New(tmplFile).Funcs( | ||
template.FuncMap{ | ||
"objectInflection": l.OjectInflection, | ||
"singularize": inflect.Singularize, | ||
"add": func(x int, y int) int { | ||
return x + y | ||
}, | ||
"subtract": func(x int, y int) int { | ||
return x - y | ||
}, | ||
"repeat": func(s string, n int) string { | ||
return strings.Repeat(s, n) | ||
}, | ||
}, | ||
).ParseFiles( | ||
path.Join("templates", l.Directory, "library", tmplFile), | ||
path.Join("templates", l.Directory, "library", "_shared.tmpl"), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
fileName := fmt.Sprintf("%s.%s", l.FileNameInflection(name), l.FileExtension) | ||
dest := dir | ||
if tmplType != "dashboard" && tmplType != "main" { | ||
dest = path.Join(dir, tmplType) | ||
} | ||
f, err := os.Create(path.Join(dest, fileName)) | ||
if err != nil { | ||
return err | ||
} | ||
return tmpl.Execute(f, data) | ||
} | ||
|
||
// Generate dashboard file. | ||
err := g("dashboard", "dashboard", s.Components.Schemas["Dashboard"]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Generate panel files. | ||
for n, sc := range s.Components.Schemas["Panel"].Properties { | ||
err = g(n, "panel", *sc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Generate target files. | ||
for n, sc := range s.Components.Schemas["Target"].Properties { | ||
err = g(n, "target", *sc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Generate template files. | ||
for n, sc := range s.Components.Schemas["Template"].Properties { | ||
err = g(n, "template", *sc) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Generate main. | ||
err = g("grafana", "main", s.Components.Schemas) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Generate docs. | ||
tmpl, err := template.New("docs.tmpl").Funcs( | ||
template.FuncMap{ | ||
"objectInflection": l.OjectInflection, | ||
"singularize": inflect.Singularize, | ||
"inflectJoin": func(elems ...string) (s string) { | ||
for i, e := range elems { | ||
elems[i] = l.OjectInflection(e) | ||
} | ||
return strings.Join(elems, ".") | ||
}, | ||
"indent": func(spaces int, s string) string { | ||
pad := strings.Repeat(" ", spaces) | ||
return pad + strings.Replace(s, "\n", "\n"+pad, -1) | ||
}, | ||
}, | ||
).ParseFiles(path.Join("templates", "docs.tmpl")) | ||
if err != nil { | ||
return err | ||
} | ||
f, err := os.Create(path.Join(dir, "DOCS.md")) | ||
if err != nil { | ||
return err | ||
} | ||
return tmpl.Execute(f, s.Components.Schemas) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Would like to keep the Go code language agnostic.