From ed35008a2932d146d45bcd59dd9e5d2b5783c048 Mon Sep 17 00:00:00 2001 From: Bart Van Bos Date: Sun, 15 Nov 2026 15:49:33 +0100 Subject: [PATCH] fix(eds): keep the GenerateGo header on one line for any source name Co-Authored-By: Claude Opus 5.5 --- RELEASE.md | 29 +++++++++++++++++++++++++++++ docs/api/eds.md | 3 ++- eds/generate.go | 5 +++-- eds/generate_test.go | 26 ++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index 0e71f9e..b14bd00 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,5 +1,34 @@ # go-ethernetip Releases +## v0.8.1 + +**Date:** 2026-11-15 +**Previous release:** v0.8.0 + +## Summary + +A patch release on v0.8.0. No exported identifier is added, removed or +changed. + +## Changes + +### Fixes + +- **eds**: `GenerateGo` writes `GenerateOptions.Source` into the header + comment as the other text of an EDS file is written into comments: + control characters, invalid UTF-8 and the byte order mark become spaces. + A source name with a line break made the header end early, so the + output did not format (an error) or held the rest of the name as code. + The header is now always the first line. + +### Tests + +- `TestGenerateSourceKeepsHeaderOneLine`: source names with LF, CR LF, + an escape sequence, U+2028, a backtick, invalid UTF-8 and `*/` give a + one-line header and the same file after it as a plain name. + +--- + ## v0.8.0 **Date:** 2026-11-14 diff --git a/docs/api/eds.md b/docs/api/eds.md index 7bb65dc..3e85495 100644 --- a/docs/api/eds.md +++ b/docs/api/eds.md @@ -330,7 +330,8 @@ Section returns the section named name (case-insensitive), or nil. type GenerateOptions struct { // Package is the package name of the generated file; default "device". Package string - // Source names the EDS file in the header comment. + // Source names the EDS file in the header comment. Control characters in it + // become spaces, so the header stays one line. Source string // License, when set, is written as an SPDX-License-Identifier line // ("MIT"); the generated code is otherwise unlabelled, as its licence diff --git a/eds/generate.go b/eds/generate.go index 8405661..e2d0a69 100644 --- a/eds/generate.go +++ b/eds/generate.go @@ -21,7 +21,8 @@ import ( type GenerateOptions struct { // Package is the package name of the generated file; default "device". Package string - // Source names the EDS file in the header comment. + // Source names the EDS file in the header comment. Control characters in it + // become spaces, so the header stays one line. Source string // License, when set, is written as an SPDX-License-Identifier line // ("MIT"); the generated code is otherwise unlabelled, as its licence @@ -154,7 +155,7 @@ func (g *gen) header(dev Device, opts GenerateOptions) { if src == "" { src = "an EDS file" } - g.p("// Code generated by go-ethernetip eds.GenerateGo from %s. DO NOT EDIT.\n\n", src) + g.p("// Code generated by go-ethernetip eds.GenerateGo from %s. DO NOT EDIT.\n\n", goComment(src)) if opts.License != "" { g.p("// SPDX-License-Identifier: %s\n\n", strings.Map(func(r rune) rune { if r == '\n' || r == '\r' { diff --git a/eds/generate_test.go b/eds/generate_test.go index 98d8f48..32d32fb 100644 --- a/eds/generate_test.go +++ b/eds/generate_test.go @@ -85,6 +85,32 @@ func TestGenerateGolden(t *testing.T) { } } +// The source name is a file name the caller passes on: whatever it holds, the +// header stays the first line and the declarations are those of a plain name. +func TestGenerateSourceKeepsHeaderOneLine(t *testing.T) { + plain, err := GenerateGo(sample(t), GenerateOptions{Source: "device.eds"}) + if err != nil { + t.Fatal(err) + } + for _, src := range []string{"a\nb.eds", "a\r\nb.eds", "a\x1b[2Jb.eds", "a\u2028b`.eds", "a\xffb.eds", "a */ b.eds"} { + out, err := GenerateGo(sample(t), GenerateOptions{Source: src}) + if err != nil { + t.Fatalf("source %q: %v", src, err) + } + first, rest, _ := bytes.Cut(out, []byte("\n")) + if !bytes.HasPrefix(first, []byte("// Code generated by go-ethernetip eds.GenerateGo from ")) || !bytes.HasSuffix(first, []byte(". DO NOT EDIT.")) { + t.Errorf("source %q: first line %q", src, first) + } + if bytes.ContainsFunc(first, func(r rune) bool { return r < 0x20 || r == 0x7f }) { + t.Errorf("source %q: control character in the header %q", src, first) + } + _, plainRest, _ := bytes.Cut(plain, []byte("\n")) + if !bytes.Equal(rest, plainRest) { + t.Errorf("source %q: the file after the header differs from the one for a plain name", src) + } + } +} + func TestGeneratePackage(t *testing.T) { out, err := GenerateGo(sample(t), GenerateOptions{}) if err != nil {