Skip to content

Commit

Permalink
Merge pull request #87 from ucan-wg/chore-eliminate-jen-dependency
Browse files Browse the repository at this point in the history
chore(delegationtest): eliminate dependency on dave/jennifer/jen package
  • Loading branch information
smoyer64 authored Nov 28, 2024
2 parents 15535d3 + 5b7a63a commit dff52f8
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 73 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/ucan-wg/go-ucan
go 1.23

require (
github.com/dave/jennifer v1.7.1
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0
github.com/ipfs/go-cid v0.4.1
github.com/ipld/go-ipld-prime v0.21.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/dave/jennifer v1.7.1 h1:B4jJJDHelWcDhlRQxWeo0Npa/pYKBLrirAQoTN45txo=
github.com/dave/jennifer v1.7.1/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
54 changes: 31 additions & 23 deletions token/delegation/delegationtest/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"fmt"
"os"
"path/filepath"
"slices"
"time"

"github.com/dave/jennifer/jen"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/crypto"

Expand All @@ -30,7 +30,6 @@ var constantNonce = []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
type newDelegationParams struct {
privKey crypto.PrivKey
aud did.DID
sub did.DID
cmd command.Command
pol policy.Policy
opts []delegation.Option
Expand Down Expand Up @@ -203,36 +202,45 @@ func (g *generator) createProofChain(name string, prf []cid.Cid) {
}

func (g *generator) writeGoFile() error {
file := jen.NewFile("delegationtest")
file.HeaderComment("Code generated by delegationtest - DO NOT EDIT.")
var err error

refs := map[cid.Cid]string{}
f, err := os.Create("../token_gen.go")
if err != nil {
return err
}

defer func() {
err = f.Close()
}()

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 \"github.com/ipfs/go-cid\"")

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

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

file.Var().Defs(
jen.Id(d.name+"CID").Op("=").Qual("github.com/ipfs/go-cid", "MustParse").Call(jen.Lit(d.id.String())),
jen.Id(d.name).Op("=").Id("mustGetDelegation").Call(jen.Id(d.name+"CID")),
)
file.Line()
fmt.Fprintln(f)
fmt.Fprintln(f, "var (")
fmt.Fprintf(f, "\t%s = cid.MustParse(\"%s\")\n", d.name+"CID", d.id.String())
fmt.Fprintf(f, "\t%s = mustGetDelegation(%s)\n", d.name, d.name+"CID")
fmt.Fprintln(f, ")")
}

for _, c := range g.chains {
g := jen.CustomFunc(jen.Options{
Multi: true,
Separator: ",",
Close: "\n",
}, func(g *jen.Group) {
slices.Reverse(c.prf)
for _, p := range c.prf {
g.Id(refs[p])
}
})
fmt.Fprintln(f)
fmt.Fprintf(f, "var %s = []cid.Cid{\n", c.name)

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

file.Var().Id(c.name).Op("=").Index().Qual("github.com/ipfs/go-cid", "Cid").Values(g)
file.Line()
fmt.Fprintln(f, "}")
}

return file.Save("../token_gen.go")
return err
}
Loading

0 comments on commit dff52f8

Please sign in to comment.