Skip to content

Commit

Permalink
Merge pull request #92 from ucan-wg/generator-cleanup
Browse files Browse the repository at this point in the history
delegationtest: run go fmt, less noisy code
  • Loading branch information
MichaelMure authored Dec 5, 2024
2 parents 7f1adbd + 78825f4 commit c518c66
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 66 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
85 changes: 43 additions & 42 deletions token/delegation/delegationtest/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"bytes"
"fmt"
"go/format"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -202,72 +204,71 @@ func (g *generator) createProofChain(name string, prf []cid.Cid) {
}

func (g *generator) writeGoFile() error {
var err error
buf := bytes.NewBuffer(nil)

f, err := os.Create("../token_gen.go")
if err != nil {
return err
}

defer func() {
err = f.Close()
}()
Println := func(a ...any) { _, _ = fmt.Fprintln(buf, a...) }
Printf := func(format string, a ...any) { _, _ = fmt.Fprintf(buf, format, a...) }

_, _ = fmt.Fprintln(f, "// Code generated by delegationtest - DO NOT EDIT.")
_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintln(f, "package delegationtest")
_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintln(f, "import (")
_, _ = fmt.Fprintln(f, "\t\"github.com/ipfs/go-cid\"")
_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintln(f, "\t\"github.com/ucan-wg/go-ucan/token/delegation\"")
_, _ = fmt.Fprintln(f, ")")
Println("// Code generated by delegationtest - DO NOT EDIT.")
Println()
Println("package delegationtest")
Println()
Println("import (")
Println("\t\"github.com/ipfs/go-cid\"")
Println()
Println("\t\"github.com/ucan-wg/go-ucan/token/delegation\"")
Println(")")

refs := make(map[cid.Cid]string, len(g.dlgs))

for _, d := range g.dlgs {
refs[d.id] = d.name + "CID"

_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintln(f, "var (")
_, _ = fmt.Fprintf(f, "\t%sCID = cid.MustParse(\"%s\")\n", d.name, d.id.String())
_, _ = fmt.Fprintf(f, "\t%sSealed = mustGetBundle(%s).Sealed\n", d.name, d.name+"CID")
_, _ = fmt.Fprintf(f, "\t%sBundle = mustGetBundle(%s)\n", d.name, d.name+"CID")
_, _ = fmt.Fprintf(f, "\t%s = mustGetBundle(%s).Decoded\n", d.name, d.name+"CID")
_, _ = fmt.Fprintln(f, ")")
Println()
Println("var (")
Printf("\t%sCID = cid.MustParse(\"%s\")\n", d.name, d.id.String())
Printf("\t%sSealed = mustGetBundle(%s).Sealed\n", d.name, d.name+"CID")
Printf("\t%sBundle = mustGetBundle(%s)\n", d.name, d.name+"CID")
Printf("\t%s = mustGetBundle(%s).Decoded\n", d.name, d.name+"CID")
Println(")")
}

_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintln(f, "var AllTokens = []*delegation.Token{")
Println()
Println("var AllTokens = []*delegation.Token{")
for _, d := range g.dlgs {
_, _ = fmt.Fprintf(f, "\t%s,\n", d.name)
Printf("\t%s,\n", d.name)
}
_, _ = fmt.Fprintln(f, "}")
Println("}")

_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintln(f, "var AllBundles = []*delegation.Bundle{")
Println()
Println("var AllBundles = []*delegation.Bundle{")
for _, d := range g.dlgs {
_, _ = fmt.Fprintf(f, "\t%sBundle,\n", d.name)
Printf("\t%sBundle,\n", d.name)
}
_, _ = fmt.Fprintln(f, "}")
Println("}")

_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintln(f, "var cidToName = map[cid.Cid]string{")
Println()
Println("var cidToName = map[cid.Cid]string{")
for _, d := range g.dlgs {
_, _ = fmt.Fprintf(f, "\t%sCID: \"%s\",\n", d.name, d.name)
Printf("\t%sCID: \"%s\",\n", d.name, d.name)
}
_, _ = fmt.Fprintln(f, "}")
Println("}")

for _, c := range g.chains {
_, _ = fmt.Fprintln(f)
_, _ = fmt.Fprintf(f, "var %s = []cid.Cid{\n", c.name)
Println()
Printf("var %s = []cid.Cid{\n", c.name)

for _, d := range slices.Backward(c.prf) {
_, _ = fmt.Fprintf(f, "\t%s,\n", refs[d])
Printf("\t%s,\n", refs[d])
}

_, _ = fmt.Fprintln(f, "}")
Println("}")
}

out, err := format.Source(buf.Bytes())
if err != nil {
return err
}

return err
return os.WriteFile("../token_gen.go", out, 0666)
}
48 changes: 24 additions & 24 deletions token/delegation/delegationtest/token_gen.go

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

0 comments on commit c518c66

Please sign in to comment.