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

Fix incomplete support for interface encoding/binary.AppendByteOrder #2

Merged
merged 2 commits into from
Oct 18, 2023
Merged
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
23 changes: 23 additions & 0 deletions compile_go1.19_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build go1.19 && !generate
// +build go1.19,!generate

package endian_test

import (
"encoding/binary"
"testing"

"github.com/dolmen-go/endian"
)

// If endian.Native is not fully defined, this file should not compile.
// Check for support of all encoding/binary interfaces.
var compileTestAppend interface {
binary.ByteOrder
binary.AppendByteOrder
} = endian.Native

// TestCompile119 is a dummy test: the real test is above.
func TestCompileAppendByteOrder(t *testing.T) {
_ = compileTest
}
24 changes: 19 additions & 5 deletions generate_go1.19.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ var Native = binary.{{if .Big}}Big{{else}}Little{{end}}Endian

const templateOthers = `// Code generated by generate_go1.19.go; DO NOT EDIT.

{{/**/}}//go:build {{range .}}!{{.}} && {{end}}!generate
{{/**/}}// +build {{range .}}!{{.}},{{end}}!generate
{{define "go1.19"}}{{if ne . "1.19"}}!{{end}}go1.19{{end -}}

{{/**/}}//go:build {{template "go1.19" .Go}} && {{range .Tags}}!{{.}} && {{end}}!generate
{{/**/}}// +build {{template "go1.19" .Go}},{{range .Tags}}!{{.}},{{end}}!generate

package endian

Expand All @@ -212,7 +214,10 @@ import (
// Native is the byte order of GOARCH.
// It will be determined at runtime because it was unknown at code
// generation time.
var Native binary.ByteOrder
var Native {{if eq .Go "1.19"}}interface {
binary.ByteOrder
binary.AppendByteOrder
}{{else}}binary.ByteOrder{{end}}

func init() {
// http://grokbase.com/t/gg/golang-nuts/129jhmdb3d/go-nuts-how-to-tell-endian-ness-of-machine#20120918nttlyywfpl7ughnsys6pm4pgpe
Expand All @@ -228,7 +233,16 @@ func init() {

sort.Strings(knownArchs)

if err = codegen.MustParse(templateOthers).CreateFile("others.go", knownArchs); err != nil {
log.Fatal(err)
for _, f := range []*struct {
file string
data interface{}
}{
{"others.go", map[string]any{"Tags": knownArchs, "Go": ""}},
{"others_go1.19.go", map[string]any{"Tags": knownArchs, "Go": "1.19"}},
} {
if err = codegen.MustParse(templateOthers).CreateFile(f.file, f.data); err != nil {
log.Fatalf("%s: %v", f.file, err)
}
}

}
4 changes: 2 additions & 2 deletions others.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions others_go1.19.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading