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

refactor(id-cmd): split up id files into multiple files #680

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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-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