Skip to content

Commit

Permalink
Remove generated Motoko files.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Jun 3, 2024
1 parent f82b9e0 commit 10a522a
Show file tree
Hide file tree
Showing 38 changed files with 1,712 additions and 2,962 deletions.
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.

Loading

0 comments on commit 10a522a

Please sign in to comment.