gen

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	AgentName          string
	ModulePath         string
	CanisterName       string
	CanisterID         *principal.Principal
	PackageName        string
	ServiceDescription did.Description
	// contains filtered or unexported fields
}

Generator is a generator for a given service description.

func NewGenerator

func NewGenerator(agentName, canisterName, packageName string, rawDID []rune) (*Generator, error)

NewGenerator creates a new generator for the given service description.

Example
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/gen"
)

func main() {
	g, err := gen.NewGenerator("test", "test", "test", []rune("service : { inc: () -> (nat) }"))
	if err != nil {
		panic(err)
	}
	raw, err := g.Generate()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(raw))
}
Output:

// Package test provides a client for the "test" canister.
// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go.
package test

import (
    "github.com/aviate-labs/agent-go"
    "github.com/aviate-labs/agent-go/candid/idl"
    "github.com/aviate-labs/agent-go/principal"
)

// TestAgent is a client for the "test" canister.
type TestAgent struct {
    *agent.Agent
    CanisterId principal.Principal
}

// NewTestAgent creates a new agent for the "test" canister.
func NewTestAgent(canisterId principal.Principal, config agent.Config) (*TestAgent, error) {
    a, err := agent.New(config)
    if err != nil {
        return nil, err
    }
    return &TestAgent{
        Agent:      a,
        CanisterId: canisterId,
    }, nil
}

// Inc calls the "inc" method on the "test" canister.
func (a TestAgent) Inc() (*idl.Nat, error) {
    var r0 idl.Nat
    if err := a.Agent.Call(
        a.CanisterId,
        "inc",
        []any{},
        []any{&r0},
    ); err != nil {
        return nil, err
    }
    return &r0, nil
}
Example (Indirect)
package main

import (
	"fmt"

	"github.com/aviate-labs/agent-go/gen"
)

func main() {
	g, err := gen.NewGenerator("test", "test", "test", []rune("service : { inc: () -> (nat) }"))
	if err != nil {
		panic(err)
	}
	raw, err := g.Indirect().Generate()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(raw))
}
Output:

// Package test provides a client for the "test" canister.
// Do NOT edit this file. It was automatically generated by https://github.com/aviate-labs/agent-go.
package test

import (
    "github.com/aviate-labs/agent-go"
    "github.com/aviate-labs/agent-go/candid/idl"
    "github.com/aviate-labs/agent-go/principal"
)

// TestAgent is a client for the "test" canister.
type TestAgent struct {
    *agent.Agent
    CanisterId principal.Principal
}

// NewTestAgent creates a new agent for the "test" canister.
func NewTestAgent(canisterId principal.Principal, config agent.Config) (*TestAgent, error) {
    a, err := agent.New(config)
    if err != nil {
        return nil, err
    }
    return &TestAgent{
        Agent:      a,
        CanisterId: canisterId,
    }, nil
}

// Inc calls the "inc" method on the "test" canister.
func (a TestAgent) Inc() (*idl.Nat, error) {
    var r0 idl.Nat
    if err := a.Agent.Call(
        a.CanisterId,
        "inc",
        []any{},
        []any{&r0},
    ); err != nil {
        return nil, err
    }
    return &r0, nil
}

// IncCall creates an indirect representation of the "inc" method on the "test" canister.
func (a TestAgent) IncCall() (*agent.CandidAPIRequest, error) {
    return a.Agent.CreateCandidAPIRequest(
        agent.RequestTypeCall,
        a.CanisterId,
        "inc",
    )
}

func (*Generator) Generate

func (g *Generator) Generate() ([]byte, error)

func (*Generator) Indirect added in v0.4.3

func (g *Generator) Indirect() *Generator

Indirect sets the generator to generate indirect calls.

func (*Generator) WithCanisterID added in v0.7.0

func (g *Generator) WithCanisterID(canisterID *principal.Principal) *Generator

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL