Skip to content

Commit

Permalink
Add support of AppendByteOrder for unknown architectures
Browse files Browse the repository at this point in the history
Unknown architectures (where byte order is determined at runtime) get
support of AppendByteOrder interface (added in Go 1.19) in addition to
ByteOrder interface.
  • Loading branch information
dolmen committed Oct 17, 2023
1 parent 0475100 commit 7c866fc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
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.

0 comments on commit 7c866fc

Please sign in to comment.