Skip to content

Commit

Permalink
refactor(id-cmd): split up id files into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Dec 10, 2024
1 parent e74223a commit 8756c0b
Show file tree
Hide file tree
Showing 11 changed files with 9,577 additions and 9,427 deletions.
94 changes: 70 additions & 24 deletions cmd/id/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/csv"
"flag"
"go/format"
"golang.org/x/exp/maps"
"log"
"os"
"strings"
Expand All @@ -19,8 +20,11 @@ func main() {
log.SetFlags(0)

in := flag.String("in", "schema/NodeIds.csv", "path to NodeIds.csv")
out := flag.String("out", "id/id_gen.go", "path to generated file")
out := flag.String("out", "id/id_*_gen.go", "path to generated file")
flag.Parse()
if !strings.ContainsRune(*out, '*') {
log.Fatal("out must contain a wildcard")
}

if *in == "" {
log.Fatal("-in is required")
Expand All @@ -44,52 +48,94 @@ func main() {
rows[i][0] = goName(rows[i][0])
}

var b bytes.Buffer
if err := tmpl.Execute(&b, rows); err != nil {
log.Fatalf("Error generating code: %v", err)
groupedRows := map[string][][]string{}
for _, row := range rows {
nodeClass := row[2]
groupedRows[nodeClass] = append(groupedRows[nodeClass], row)
}

bfmt, err := format.Source(b.Bytes())
if err != nil {
log.Fatalf("Error formatting code: %v", err)
for nodeClass, rows := range groupedRows {
out := strings.ReplaceAll(*out, "*", nodeClass)
var b bytes.Buffer
if err := idTmpl.Execute(&b, struct {
NodeClass string
Rows [][]string
}{nodeClass, rows}); err != nil {
log.Fatalf("Error generating code: %v", err)
}

bfmt, err := format.Source(b.Bytes())
if err != nil {
log.Fatalf("Error formatting code: %v", err)
}

if err := os.WriteFile(out, bfmt, 0644); err != nil {
log.Fatalf("Error writing %s: %v", out, err)
}
log.Printf("Wrote %s", out)
}

if err := os.WriteFile(*out, bfmt, 0644); err != nil {
log.Fatalf("Error writing %s: %v", *out, err)
{
out := strings.ReplaceAll(*out, "*", "names")
var b bytes.Buffer
if err := nameTmpl.Execute(&b, maps.Keys(groupedRows)); err != nil {
log.Fatalf("Error generating code: %v", err)
}

bfmt, err := format.Source(b.Bytes())
if err != nil {
log.Fatalf("Error formatting code: %v", err)
}

if err := os.WriteFile(out, bfmt, 0644); err != nil {
log.Fatalf("Error writing %s: %v", out, err)
}
log.Printf("Wrote %s", out)
}
log.Printf("Wrote %s", *out)
}

var tmpl = template.Must(template.New("").Parse(`
// Copyright 2018-2020 opcua authors. All rights reserved.
var idTmpl = template.Must(template.New("").Parse(`
// Copyright 2018-2024 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// Code generated by cmd/id. DO NOT EDIT!
package id
import "strconv"
func Name(id uint32) string {
if s, ok := name[id]; ok {
return s
}
return strconv.FormatUint(uint64(id), 10)
}
const (
{{range .}}{{index . 0}} = {{index . 1}}
{{range .Rows}}{{index . 0}} = {{index . 1}}
{{end}}
)
var name = map[uint32]string{
{{- range .}}
var name{{.NodeClass}} = map[uint32]string{
{{- range .Rows}}
{{index . 1}}: "{{index . 0}}",
{{- end}}
}
`))

var nameTmpl = template.Must(template.New("").Parse(`
// Copyright 2018-2024 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// Code generated by cmd/id. DO NOT EDIT!
package id
import "strconv"
func Name(id uint32) string {
{{- range .}}
if s, ok := name{{.}}[id]; ok {
return s
}
{{- end}}
return strconv.FormatUint(uint64(id), 10)
}
`))

func goName(s string) string {
r1 := strings.NewReplacer(
"Guid", "GUID",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
require (
github.com/google/uuid v1.3.0
github.com/pascaldekloe/goe v0.1.1
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d
golang.org/x/term v0.8.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pascaldekloe/goe v0.1.1 h1:Ah6WQ56rZONR3RW3qWa2NCZ6JAVvSpUcoLBaOmYFt9Q=
github.com/pascaldekloe/goe v0.1.1/go.mod h1:KSyfaxQOh0HZPjDP1FL/kFtbqYqrALJTaMafFUIccqU=
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
Expand Down
Loading

0 comments on commit 8756c0b

Please sign in to comment.