Skip to content

Commit

Permalink
Remove mock replica package.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 6, 2024
1 parent 29520f3 commit 6523692
Show file tree
Hide file tree
Showing 13 changed files with 0 additions and 5,210 deletions.
195 changes: 0 additions & 195 deletions gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,198 +255,12 @@ func (g *Generator) GenerateActorTypes() ([]byte, error) {
return io.ReadAll(&tmpl)
}

func (g *Generator) GenerateMock() ([]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 []agentArgsMethod
for _, service := range g.ServiceDescription.Services {
for _, method := range service.Methods {
name := rawName(method.Name)
f := method.Func

var argumentTypes []agentArgsMethodArgument
var filledArgumentTypes []agentArgsMethodArgument
for i, t := range f.ArgTypes {
var n string
if (t.Name != nil) && (*t.Name != "") {
n = *t.Name
} else {
n = fmt.Sprintf("arg%d", i)
}
argumentTypes = append(argumentTypes, agentArgsMethodArgument{
Name: n,
Type: g.dataToString(g.PackageName, t.Data),
})
filledArgumentTypes = append(filledArgumentTypes, agentArgsMethodArgument{
Name: n,
Type: g.dataToGoReturnValue(definitions, g.PackageName, t.Data),
})
}

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

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

methods = append(methods, agentArgsMethod{
RawName: name,
Name: funcName("", name),
Type: typ,
ArgumentTypes: argumentTypes,
FilledArgumentTypes: filledArgumentTypes,
ReturnTypes: returnTypes,
})
}
}
t, ok := templates["agent_test"]
if !ok {
return nil, fmt.Errorf("template not found")
}
var tmpl bytes.Buffer
if err := t.Execute(&tmpl, agentMockArgs{
AgentName: g.AgentName,
CanisterName: g.CanisterName,
PackageName: g.PackageName,
ModulePath: g.ModulePath,
UsedIDL: g.usedIDL,
Methods: methods,
}); 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) dataToGoReturnValue(definitions map[string]did.Data, prefix string, data did.Data) string {
switch t := data.(type) {
case did.Primitive:
switch t {
case "nat":
g.usedIDL = true
return "idl.NewNat(uint(0))"
case "int":
g.usedIDL = true
return "idl.NewInt(0)"
default:
return fmt.Sprintf("*new(%s)", g.dataToString(prefix, data))
}
case did.DataId:
switch data := definitions[t.String()].(type) {
case did.Record:
var fields []string
for _, f := range data {
var data did.Data
if f.Data != nil {
data = *f.Data
} else {
data = did.DataId(*f.NameData)
}
fields = append(fields, g.dataToGoReturnValue(definitions, prefix, data))
}
if len(fields) == 0 {
return fmt.Sprintf("%s{}", g.dataToString(prefix, t))
}
return fmt.Sprintf("%s{\n%s,\n}", g.dataToString(prefix, t), strings.Join(fields, ",\n"))
case did.Variant:
f := data[0]
var d did.Data
if f.Data != nil {
d = *f.Data
} else {
d = did.DataId(*f.NameData)
}
field := g.dataToGoReturnValue(definitions, prefix, d)
if !strings.HasPrefix(field, "*") {
g.usedIDL = true
field = fmt.Sprintf("idl.Ptr(%s)", field)
} else {
field = strings.TrimPrefix(field, "*")
}
var name string
if f.Name != nil {
name = *f.Name
} else {
name = *f.NameData
}
return fmt.Sprintf("%s{\n%s: %s,\n}", g.dataToString(prefix, t), funcName("", name), field)
default:
switch data := data.(type) {
case did.Primitive:
switch data {
case "nat":
g.usedIDL = true
return "idl.NewNat(uint(0))"
case "int":
g.usedIDL = true
return "idl.NewInt(0)"
}
}
if data != nil {
return fmt.Sprintf("*new(%s)", g.dataToString(prefix, data))
}
return "*new(idl.Null)"
}
case did.Record:
var fields []string
for _, f := range t {
var data did.Data
if f.Data != nil {
data = *f.Data
} else {
data = did.DataId(*f.NameData)
}
fields = append(fields, g.dataToGoReturnValue(definitions, prefix, data))
}
if len(fields) == 0 {
return fmt.Sprintf("%s{}", g.dataToString(prefix, data))
}
return fmt.Sprintf("%s{\n%s,\n}", g.dataToString(prefix, data), strings.Join(fields, ",\n"))
case did.Variant:
f := t[0]
var name string
var d did.Data
if f.Data != nil {
name = *f.Name
d = *f.Data
} else {
name = *f.NameData
d = did.DataId(*f.NameData)
}
field := g.dataToGoReturnValue(definitions, prefix, d)
if !strings.HasPrefix(field, "*") {
g.usedIDL = true
field = fmt.Sprintf("idl.Ptr(%s)", field)
} else {
field = strings.TrimPrefix(field, "*")
}
return fmt.Sprintf("%s{\n%s: %s,\n}", g.dataToString(prefix, data), funcName("", name), field)
case did.Vector:
switch t.Data.(type) {
case did.DataId:
return fmt.Sprintf("[]%s{%s}", funcName(prefix, t.Data.String()), g.dataToGoReturnValue(definitions, prefix, t.Data))
default:
return fmt.Sprintf("[]%s{%s}", g.dataToString(prefix, t.Data), g.dataToGoReturnValue(definitions, prefix, t.Data))
}
default:
return fmt.Sprintf("*new(%s)", g.dataToString(prefix, data))
}
}

func (g *Generator) dataToMotokoReturnValue(s rand.Source, definitions map[string]did.Data, data did.Data) string {
r := rand.New(s)
switch t := data.(type) {
Expand Down Expand Up @@ -809,12 +623,3 @@ type agentArgsMethodArgument struct {
Name string
Type string
}

type agentMockArgs struct {
AgentName string
CanisterName string
PackageName string
ModulePath string
UsedIDL bool
Methods []agentArgsMethod
}
57 changes: 0 additions & 57 deletions gen/templates/agent_test.gotmpl

This file was deleted.

Loading

0 comments on commit 6523692

Please sign in to comment.