Skip to content

Formatting auto-generated code. #338

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

Merged
merged 1 commit into from
Jun 6, 2022
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
30 changes: 23 additions & 7 deletions dh/sidh/internal/templates/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package main

import (
"bytes"
"fmt"
"go/format"
"os"
"strings"
"text/template"
Expand Down Expand Up @@ -47,22 +49,36 @@ var opt_arm = map[string]bool{
// Generates an 'fileNameBase.go' from 'fileNameBase.gotemp' file
// for a given finite 'field'. Maps placeholders to 'values'.
func gen(field, fileNameBase string, values interface{}) {
// Template files are located in ../templates and have
// extension .gotemp
templateFile := "../templates/" + fileNameBase + ".gotemp"
t, err := template.ParseFiles(templateFile)
if err != nil {
panic(fmt.Sprintf("Cannot open template file %s", templateFile))
}
var buf bytes.Buffer
err = t.Execute(&buf, values)
if err != nil {
panic("bad template execution")
}

// Formating output code
code, err := format.Source(buf.Bytes())
if err != nil {
panic("error formating code")
}

// name of the output .go file
outFileName := fileNameBase + ".go"
out, err := os.Create(outFileName)
if err != nil {
panic("Cannot open file")
}

// Template files are located in ../templates and have
// extension .gotemp
templateFile := "../templates/" + fileNameBase + ".gotemp"
t, err := template.ParseFiles(templateFile)
_, err = out.Write(code)
if err != nil {
panic(fmt.Sprintf("Cannot open template file %s", templateFile))
panic("error writing code")
}

t.Execute(out, values)
err = out.Close()
if err != nil {
panic("Cant close generated file")
Expand Down
9 changes: 8 additions & 1 deletion kem/kyber/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main

import (
"bytes"
"go/format"
"io/ioutil"
"strings"
"text/template"
Expand Down Expand Up @@ -47,7 +48,13 @@ func generatePackageFiles() {
panic(err)
}

res := string(buf.Bytes())
// Formating output code
code, err := format.Source(buf.Bytes())
if err != nil {
panic("error formating code")
}

res := string(code)
offset := strings.Index(res, TemplateWarning)
if offset == -1 {
panic("Missing template warning in pkg.templ.go")
Expand Down
2 changes: 1 addition & 1 deletion kem/kyber/kyber1024/kyber.go

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

2 changes: 1 addition & 1 deletion kem/kyber/kyber512/kyber.go

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

2 changes: 1 addition & 1 deletion kem/kyber/kyber768/kyber.go

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

9 changes: 8 additions & 1 deletion kem/sike/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"strings"
"text/template"
Expand Down Expand Up @@ -56,7 +57,13 @@ func generatePackageFiles() {
panic(err)
}

res := string(buf.Bytes())
// Formating output code
code, err := format.Source(buf.Bytes())
if err != nil {
panic("error formating code")
}

res := string(code)
offset := strings.Index(res, TemplateWarning)
if offset == -1 {
panic("Missing template warning in pkg.templ.go")
Expand Down
9 changes: 8 additions & 1 deletion pke/kyber/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -82,7 +83,13 @@ func generateParamsFiles() {
panic(err)
}

res := string(buf.Bytes())
// Formating output code
code, err := format.Source(buf.Bytes())
if err != nil {
panic("error formating code")
}

res := string(code)
offset := strings.Index(res, TemplateWarning)
if offset == -1 {
panic("Missing template warning in params.templ.go")
Expand Down
9 changes: 8 additions & 1 deletion sign/dilithium/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -142,7 +143,13 @@ func generateParamsFiles() {
panic(err)
}

res := string(buf.Bytes())
// Formating output code
code, err := format.Source(buf.Bytes())
if err != nil {
panic("error formating code")
}

res := string(code)
offset := strings.Index(res, TemplateWarning)
if offset == -1 {
panic("Missing template warning in params.templ.go")
Expand Down