Skip to content
Open
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
104 changes: 104 additions & 0 deletions cmd/mxsml-error-catalog/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"encoding/json"
"flag"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"sort"
"strings"
)

type item struct {
Name string `json:"name"`
Value string `json:"value"`
}

type catalog struct {
PrimaryCount int `json:"primary_count"`
ExtensionCount int `json:"extension_count"`
PrimaryFile string `json:"primary_file"`
ExtensionFile string `json:"extension_file"`
Primary []item `json:"primary"`
Extension []item `json:"extension"`
}

func collectErrors(path string, prefix string) ([]item, error) {
parsed, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.SkipObjectResolution)
if err != nil {
return nil, err
}
var errors []item
for _, declaration := range parsed.Decls {
general, ok := declaration.(*ast.GenDecl)
if !ok || general.Tok != token.CONST {
continue
}
for _, specification := range general.Specs {
valueSpec, ok := specification.(*ast.ValueSpec)
if !ok {
continue
}
for index, name := range valueSpec.Names {
if !strings.HasPrefix(name.Name, prefix) || !strings.Contains(name.Name, "ERROR") {
continue
}
value := ""
if index < len(valueSpec.Values) {
value = exprString(valueSpec.Values[index])
}
errors = append(errors, item{Name: name.Name, Value: value})
}
}
}
sort.Slice(errors, func(i, j int) bool { return errors[i].Name < errors[j].Name })
return errors, nil
}

func exprString(expression ast.Expr) string {
switch value := expression.(type) {
case *ast.BasicLit:
return value.Value
case *ast.Ident:
return value.Name
default:
return ""
}
}

func run(repoRoot string) (catalog, error) {
primaryFile := filepath.Join(repoRoot, "pkg", "mxsml", "const.go")
extensionFile := filepath.Join(repoRoot, "pkg", "mxsmlextension", "const.go")
primary, err := collectErrors(primaryFile, "MXSML_")
if err != nil {
return catalog{}, err
}
extension, err := collectErrors(extensionFile, "MXSMLEX_")
if err != nil {
return catalog{}, err
}
return catalog{
PrimaryCount: len(primary),
ExtensionCount: len(extension),
PrimaryFile: filepath.ToSlash(primaryFile),
ExtensionFile: filepath.ToSlash(extensionFile),
Primary: primary,
Extension: extension,
}, nil
}

func main() {
repoRoot := flag.String("repo-root", ".", "Repository root")
flag.Parse()

report, err := run(*repoRoot)
if err != nil {
panic(err)
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
_ = encoder.Encode(report)
}
43 changes: 43 additions & 0 deletions cmd/mxsml-error-catalog/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"os"
"path/filepath"
"testing"
)

func writeFile(t *testing.T, path string, body string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}

func TestCollectErrors(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "pkg", "mxsml", "const.go"), `package mxsml
const (
MXSML_SUCCESS = 0
MXSML_ERROR_FAILURE = 1
MXSML_ERROR_BUSY = 2
)`)
writeFile(t, filepath.Join(root, "pkg", "mxsmlextension", "const.go"), `package mxsmlextension
const (
MXSMLEX_SUCCESS = 0
MXSMLEX_ERROR_TIMEOUT = 10
)`)

report, err := run(root)
if err != nil {
t.Fatal(err)
}
if report.PrimaryCount != 2 {
t.Fatalf("unexpected primary count: %d", report.PrimaryCount)
}
if report.ExtensionCount != 1 {
t.Fatalf("unexpected extension count: %d", report.ExtensionCount)
}
}