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 2619f05
Show file tree
Hide file tree
Showing 9 changed files with 9,574 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"

Check failure on line 12 in cmd/id/main.go

View workflow job for this annotation

GitHub Actions / Test ubuntu-latest go1.23.x

no required module provides package golang.org/x/exp/maps; to add it:
"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-2023 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-2023 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
Loading

0 comments on commit 2619f05

Please sign in to comment.