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

Remove generated Motoko files. #36

Merged
merged 2 commits into from
Jun 3, 2024
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
275 changes: 0 additions & 275 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"fmt"
"github.com/aviate-labs/agent-go/candid"
"github.com/aviate-labs/agent-go/candid/did"
"github.com/aviate-labs/agent-go/candid/idl"
"io"
"io/fs"
"math/rand"
"strings"
"text/template"
)
Expand Down Expand Up @@ -169,268 +167,12 @@ func (g *Generator) Generate() ([]byte, error) {
return io.ReadAll(&tmpl)
}

func (g *Generator) GenerateActor() ([]byte, error) {
definitions := make(map[string]did.Data)
for _, definition := range g.ServiceDescription.Definitions {
switch definition := definition.(type) {
case did.Type:
definitions[definition.Id] = definition.Data
}
}

var methods []actorArgsMethod
for _, service := range g.ServiceDescription.Services {
for _, method := range service.Methods {
name := rawName(method.Name)
f := method.Func

var argumentTypes []agentArgsMethodArgument
for i, t := range f.ArgTypes {
argumentTypes = append(argumentTypes, agentArgsMethodArgument{
Name: fmt.Sprintf("_arg%d", i),
Type: g.dataToMotokoString(t.Data),
})
}

var returnTypes []string
for _, t := range f.ResTypes {
returnTypes = append(returnTypes, g.dataToMotokoString(t.Data))
}

r := rand.NewSource(idl.Hash(g.CanisterName).Int64())
var returnValues []string
for _, t := range f.ResTypes {
returnValues = append(returnValues, g.dataToMotokoReturnValue(r, definitions, t.Data))
}

typ := "shared"
if f.Annotation != nil && *f.Annotation == did.AnnQuery {
typ = "query"
}

methods = append(methods, actorArgsMethod{
Name: name,
Type: typ,
ArgumentTypes: argumentTypes,
ReturnTypes: returnTypes,
ReturnValues: returnValues,
})
}
}
t, ok := templates["actor_mo"]
if !ok {
return nil, fmt.Errorf("template not found")
}
var tmpl bytes.Buffer
if err := t.Execute(&tmpl, actorArgs{
CanisterName: g.CanisterName,
Methods: methods,
}); err != nil {
return nil, err
}
return io.ReadAll(&tmpl)
}

func (g *Generator) GenerateActorTypes() ([]byte, error) {
var definitions []agentArgsDefinition
for _, definition := range g.ServiceDescription.Definitions {
switch definition := definition.(type) {
case did.Type:
definitions = append(definitions, agentArgsDefinition{
Name: funcName("", definition.Id),
Type: g.dataToMotokoString(definition.Data),
})
}
}
t, ok := templates["types_mo"]
if !ok {
return nil, fmt.Errorf("template not found")
}
var tmpl bytes.Buffer
if err := t.Execute(&tmpl, actorTypesArgs{
Definitions: definitions,
}); err != nil {
return nil, err
}
return io.ReadAll(&tmpl)
}

// Indirect sets the generator to generate indirect calls.
func (g *Generator) Indirect() *Generator {
g.indirect = true
return g
}

func (g *Generator) dataToMotokoReturnValue(s rand.Source, definitions map[string]did.Data, data did.Data) string {
r := rand.New(s)
switch t := data.(type) {
case did.DataId:
return g.dataToMotokoReturnValue(s, definitions, definitions[string(t)])
case did.Blob:
var b [32]byte
r.Read(b[:])
return fmt.Sprintf("\"x%02X\"", b)
case did.Func:
return "{ /* func */ }"
case did.Principal:
var b [32]byte
r.Read(b[:])
return fmt.Sprintf("principalOfBlob(\"x%02X\")", b[:])
case did.Primitive:
switch t {
case "null":
return "()"
case "bool":
return fmt.Sprintf("%t", r.Int()%2 == 0)
case "nat", "int":
return fmt.Sprintf("%d", r.Uint64())
case "nat8":
return fmt.Sprintf("%d", r.Uint64()%0xFF)
case "nat16":
return fmt.Sprintf("%d", r.Uint64()%0xFFFF)
case "nat32":
return fmt.Sprintf("%d", r.Uint64()%0xFFFFFFFF)
case "nat64":
return fmt.Sprintf("%d", r.Uint64()%0xFFFFFFFFFFFFFFFF)
case "text":
return fmt.Sprintf("\"%d\"", r.Uint64())
}
case did.Vector:
n := r.Int() % 10
var values []string
for i := 0; i < n; i++ {
values = append(values, g.dataToMotokoReturnValue(s, definitions, t.Data))
}
return fmt.Sprintf("[ %s ]", strings.Join(values, ", "))
case did.Record:
var tuple bool
var fields []string
for _, v := range t {
if v.Name != nil {
var data string
if v.NameData != nil {
data = g.dataToMotokoReturnValue(s, definitions, definitions[*v.NameData])
} else {
data = g.dataToMotokoReturnValue(s, definitions, *v.Data)
}
fields = append(fields, fmt.Sprintf("%s = %s", *v.Name, data))
} else {
tuple = true
break
}
}
if !tuple {
return fmt.Sprintf("{ %s }", strings.Join(fields, "; "))
}

var values []string
for _, field := range t {
if field.Data != nil {
values = append(values, g.dataToMotokoReturnValue(s, definitions, *field.Data))
} else {
values = append(values, g.dataToMotokoReturnValue(s, definitions, definitions[*field.NameData]))
}
}
return fmt.Sprintf("( %s )", strings.Join(values, ", "))
case did.Variant:
r := s.Int63() % int64(len(t))
field := t[r]
if field.Data != nil {
return fmt.Sprintf("#%s(%s)", *field.Name, g.dataToMotokoReturnValue(s, definitions, *field.Data))
}
if field.Name != nil {
return fmt.Sprintf("#%s(%s)", *field.Name, g.dataToMotokoReturnValue(s, definitions, definitions[*field.NameData]))
}
if data := definitions[*field.NameData]; data != nil {
return fmt.Sprintf("#%s(%s)", *field.NameData, g.dataToMotokoReturnValue(s, definitions, definitions[*field.NameData]))
}
return fmt.Sprintf("#%s", *field.NameData)
case did.Optional:
return fmt.Sprintf("?%s", g.dataToMotokoReturnValue(s, definitions, t.Data))
}
return fmt.Sprintf("%q # %q", "UNKNOWN", data)
}

func (g *Generator) dataToMotokoString(data did.Data) string {
switch t := data.(type) {
case did.Blob:
return "Blob"
case did.Func:
return "{ /* func */ }"
case did.Record:
var fields []string
for _, v := range t {
if v.Name != nil {
var data string
if v.NameData != nil {
data = fmt.Sprintf("T.%s", funcName("", *v.NameData))
} else {
data = g.dataToMotokoString(*v.Data)
}
fields = append(fields, fmt.Sprintf("%s : %s", *v.Name, data))
} else {
if v.NameData != nil {
fields = append(fields, fmt.Sprintf("T.%s", funcName("", *v.NameData)))
} else {
fields = append(fields, g.dataToMotokoString(*v.Data))
}
}
}
var isTuple bool
for _, f := range fields {
if !strings.Contains(f, ":") {
isTuple = true
break
}
}
if isTuple {
return fmt.Sprintf("(%s)", strings.Join(fields, ", "))
}
return fmt.Sprintf("{ %s }", strings.Join(fields, "; "))
case did.Variant:
var variants []string
for _, v := range t {
if v.Name != nil {
var data string
if v.NameData != nil {
data = fmt.Sprintf("T.%s", funcName("", *v.NameData))
} else {
data = g.dataToMotokoString(*v.Data)
}
variants = append(variants, fmt.Sprintf("#%s : %s", *v.Name, data))
} else {
variants = append(variants, fmt.Sprintf("#%s", *v.NameData))
}
}
return fmt.Sprintf("{ %s }", strings.Join(variants, "; "))
case did.Vector:
return fmt.Sprintf("[%s]", g.dataToMotokoString(t.Data))
case did.Optional:
return fmt.Sprintf("?%s", g.dataToMotokoString(t.Data))
case did.Primitive:
switch t {
case "nat", "nat8", "nat16", "nat32", "nat64":
return strings.ReplaceAll(data.String(), "nat", "Nat")
case "int", "int8", "int16", "int32", "int64":
return strings.ReplaceAll(data.String(), "int", "Int")
case "text":
return "Text"
case "bool":
return "Bool"
case "null":
return "()"
default:
return t.String()
}
case did.Principal:
return "Principal"
case did.DataId:
return fmt.Sprintf("T.%s", funcName("", string(t)))
default:
panic(fmt.Sprintf("unknown type: %T", t))
}
}

func (g *Generator) dataToString(prefix string, data did.Data) string {
switch t := data.(type) {
case did.Blob:
Expand Down Expand Up @@ -578,23 +320,6 @@ func (g *Generator) dataToString(prefix string, data did.Data) string {
}
}

type actorArgs struct {
CanisterName string
Methods []actorArgsMethod
}

type actorArgsMethod struct {
Name string
Type string
ArgumentTypes []agentArgsMethodArgument
ReturnTypes []string
ReturnValues []string
}

type actorTypesArgs struct {
Definitions []agentArgsDefinition
}

type agentArgs struct {
AgentName string
CanisterName string
Expand Down
12 changes: 0 additions & 12 deletions gen/templates/actor_mo.gotmpl

This file was deleted.

6 changes: 0 additions & 6 deletions gen/templates/types_mo.gotmpl

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a
github.com/di-wu/parser v0.3.0
github.com/fxamacker/cbor/v2 v2.6.0
github.com/herumi/bls-go-binary v1.33.0
github.com/herumi/bls-go-binary v1.34.0
google.golang.org/protobuf v1.34.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1t
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/herumi/bls-go-binary v1.33.0 h1:OJwWkXTsxF7SLHT8cBLJfb6i97KHfrG4DkgejLcDm78=
github.com/herumi/bls-go-binary v1.33.0/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y=
github.com/herumi/bls-go-binary v1.34.0 h1:x1sKp8zzx+alvlifB+vbA0KkY0Pz4Br31cZ/saDkiFE=
github.com/herumi/bls-go-binary v1.34.0/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down
Loading
Loading