Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Generate jsonnet from json #3

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ steps:
- name: generate
image: golang:1.14.6-alpine3.12
commands:
- go run . 7.0 jsonnet
- go run . library 7.0 jsonnet

- name: jsonnet fmt
image: bitnami/jsonnet:0.16.0
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
_gen
dashboard-spec
108 changes: 108 additions & 0 deletions instance.go
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"
Copy link
Member

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.


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use objectInflection where this might be used?

"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
}
119 changes: 119 additions & 0 deletions library.go
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)
}
Loading