Skip to content
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

add script for generating error constants #35

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module scripts/errors-const/
112 changes: 112 additions & 0 deletions scripts/errors-const/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"sort"
"strings"
"text/template"
)

type options struct {
jsonSource string
template string
output string
}

const version = "0.1.0"

func main() {
o := &options{}
flag.StringVar(&o.jsonSource, "json", "", `path to the ably-common/protocol/errors.json`)
flag.StringVar(&o.template, "t", "", "path to the template file")
flag.StringVar(&o.output, "o", "", "file to write output")
flag.Parse()
if o.jsonSource == "" {
flag.PrintDefaults()
return
}
if o.template == "" {
fmt.Println("[error] missing -t flag")
flag.PrintDefaults()
os.Exit(1)
}
tplData, err := ioutil.ReadFile(o.template)
if err != nil {
log.Fatal(err)
}
var out io.Writer
if o.output != "" {
f, err := os.Open(o.output)
if err != nil {
log.Fatal(err)
}
defer f.Close()
out = f
} else {
out = os.Stdout
}
f, err := ioutil.ReadFile(o.jsonSource)
if err != nil {
log.Fatal(err)
}
re := regexp.MustCompile(`\/\*([\s\S]*?)\*\/`)

// we remove the multiline comments
f = re.ReplaceAll(f, []byte{})
m := make(map[int]string)
err = json.Unmarshal(f, &m)
if err != nil {
log.Fatal(err)
}
var keys []int
Copy link

Choose a reason for hiding this comment

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

More descriptive variable names woulod be good. Examples/suggestions: o --> opts, f --> outpFile m --> codeToDesc, keys --> codes, etc.

for k := range m {
keys = append(keys, k)
}
sort.Ints(keys)
tpl, err := template.New("errors").Funcs(template.FuncMap{
"key": func(k int) string {
return m[k]
},
"normalize": normalize,
"split": func(a, b string) []string {
return strings.Split(b, a)
},
"join": func(sep string, v []string) string {
return strings.Join(v, sep)
},
"title": func(v []string) []string {
for k := range v {
v[k] = strings.Title(v[k])
}
return v
},
}).Parse(string(tplData))
if err != nil {
log.Fatal(err)
}
err = tpl.Execute(out, map[string]interface{}{
"codes": keys,
"version": version,
})
if err != nil {
log.Fatal(err)
}
}

func normalize(v string) string {
idx := strings.Index(v, ")")
if idx != -1 {
v = v[:idx]
v = strings.Replace(v, "(", " ", -1)
v = strings.Replace(v, ")", " ", -1)
}
v = strings.Replace(v, "-", " ", -1)
return v
}
10 changes: 10 additions & 0 deletions scripts/errors-const/templates/go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Code generated by errors-const v{{.version}} tool DO NOT EDIT.

package ably

// ably error codes
const(
{{- range .codes}}
Err{{key .|normalize|split " "|title|join ""}} = {{.}}
{{- end}}
)