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 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
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/
16 changes: 16 additions & 0 deletions scripts/errors-const/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# errors-const

Is a tool that generates ably error codes constants for different supported
languages.
This tool uses go templates to generate the output.

## Usage
```
$ errors-const
-json string
path to the ably-common/protocol/errors.json
-o string
file to write output
-t string
path to the template file
```
123 changes: 123 additions & 0 deletions scripts/errors-const/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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() {
opts := &options{}
flag.StringVar(&opts.jsonSource, "json", "", `path to the ably-common/protocol/errors.json`)
flag.StringVar(&opts.template, "t", "", "path to the template file")
flag.StringVar(&opts.output, "o", "", "file to write output")
flag.Parse()
if opts.jsonSource == "" {
flag.PrintDefaults()
return
}
if opts.template == "" {
fmt.Println("[error] missing -t flag")
flag.PrintDefaults()
os.Exit(1)
}
tplData, err := ioutil.ReadFile(opts.template)
if err != nil {
log.Fatal(err)
}
var out io.Writer
if opts.output != "" {
f, err := os.Open(opts.output)
if err != nil {
log.Fatal(err)
}
defer f.Close()
out = f
} else {
out = os.Stdout
}
sourceFile, err := ioutil.ReadFile(opts.jsonSource)
if err != nil {
log.Fatal(err)
}
re := regexp.MustCompile(`\/\*([\s\S]*?)\*\/`)

// we remove the multiline comments
sourceFile = re.ReplaceAll(sourceFile, []byte{})
codeToDesc := make(map[int]string)
err = json.Unmarshal(sourceFile, &codeToDesc)
if err != nil {
log.Fatal(err)
}
var codes []int
for k := range codeToDesc {
codes = append(codes, k)
}
sort.Ints(codes)
tpl, err := template.New("errors").Funcs(template.FuncMap{
"key": func(k int) string {
return codeToDesc[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
},
"map": func(from, to string, v []string) []string {
dest := make([]string, len(v))
for k := range v {
if v[k] == from {
dest = append(dest, to)
} else {
dest = append(dest, v[k])
}
}
return dest
},
}).Parse(string(tplData))
if err != nil {
log.Fatal(err)
}
err = tpl.Execute(out, map[string]interface{}{
"codes": codes,
"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 " "|map "id" "ID"|map "clientId" "clientID"|map "tls" "TLS"|title|join ""}} = {{.}}
{{- end}}
)