quickstart API

quickstart

package

API reference for the quickstart package.

S
struct

User

User is the model returned by the example handlers.

examples/quickstart/quickstart.go:14-17
type User struct

Fields

Name Type Description
ID int json:"id"
Name string json:"name"
I
interface

UserStore

UserStore is the application contract used by the example.

examples/quickstart/quickstart.go:20-23
type UserStore interface

Methods

Create
Method

Parameters

name string

Returns

func Create(...)
Find
Method

Parameters

id int

Returns

bool
func Find(...)
S
struct
Implements: UserStore

MemoryUserStore

MemoryUserStore is an in-memory UserStore implementation.

examples/quickstart/quickstart.go:26-32
type MemoryUserStore struct

Methods

Create
Method

Create stores a user.

Parameters

name string

Returns

func (*MemoryUserStore) Create(name string) User
{
	s.mu.Lock()
	defer s.mu.Unlock()

	user := User{ID: s.next, Name: name}
	s.users[user.ID] = user
	s.next++
	return user
}
Find
Method

Find returns a user by ID.

Parameters

id int

Returns

bool
func (*MemoryUserStore) Find(id int) (User, bool)
{
	s.mu.RLock()
	defer s.mu.RUnlock()

	user, ok := s.users[id]
	return user, ok
}

Fields

Name Type Description
mu sync.RWMutex
users map[int]User
next int
F
function

NewMemoryUserStore

NewMemoryUserStore creates an empty store.

Returns

examples/quickstart/quickstart.go:35-40
func NewMemoryUserStore() *MemoryUserStore

{
	return &MemoryUserStore{
		users: make(map[int]User),
		next:  1,
	}
}
S
struct

GetUser

GetUser is a generated HTTP registration target.

examples/quickstart/quickstart.go:63-67
type GetUser struct

Methods

Handle
Method

Handle returns the requested user.

Parameters

Returns

any
error
func (*GetUser) Handle(context.Context) (any, error)
{
	user, ok := h.Users.Find(h.ID)
	if !ok {
		return nil, fmt.Errorf("user %d not found", h.ID)
	}
	return user, nil
}

Fields

Name Type Description
_ struct{} method:"GET" path:"/users/{id:int}"
ID int path:"id"
Users UserStore inject:"users"
S
struct

CreateUser

CreateUser is a generated action registration target.

examples/quickstart/quickstart.go:79-83
type CreateUser struct

Methods

Handle
Method

Handle creates a user.

Parameters

Returns

any
error
func (*CreateUser) Handle(context.Context) (any, error)
{
	if a.Name == "" {
		return nil, fmt.Errorf("user name is required")
	}
	return a.Users.Create(a.Name), nil
}

Fields

Name Type Description
_ struct{} action:"users.create" keys:"ctrl+n"
Name string json:"name"
Users UserStore inject:"users"
F
function

Build

Build creates a ready application using generated registrations.

Returns

error
examples/quickstart/quickstart.go:94-102
func Build() (*app.App, error)

{
	application := app.New().
		Provide("users", NewMemoryUserStore())
	RegisterFoundation(application)
	if _, err := application.Build(); err != nil {
		return nil, err
	}
	return application, nil
}
F
function

TestBuildAndDispatchGeneratedAction

Parameters

examples/quickstart/quickstart_test.go:8-29
func TestBuildAndDispatchGeneratedAction(t *testing.T)

{
	application, err := Build()
	if err != nil {
		t.Fatalf("Build() error = %v", err)
	}

	result, err := application.Dispatch(
		context.Background(),
		"users.create",
		map[string]any{"Name": "Ada"},
	)
	if err != nil {
		t.Fatalf("Dispatch() error = %v", err)
	}
	user, ok := result.(User)
	if !ok {
		t.Fatalf("Dispatch() result type = %T", result)
	}
	if user.ID != 1 || user.Name != "Ada" {
		t.Fatalf("Dispatch() result = %#v", user)
	}
}
F
function

TestGeneratedDefinitions

Parameters

examples/quickstart/quickstart_test.go:31-41
func TestGeneratedDefinitions(t *testing.T)

{
	handlers := FoundationHTTPHandlers()
	if len(handlers) != 1 || handlers[0].Method != "GET" || handlers[0].Path != "/users/{id:int}" {
		t.Fatalf("FoundationHTTPHandlers() = %#v", handlers)
	}

	actions := FoundationActions()
	if len(actions) != 1 || actions[0].Name != "users.create" || actions[0].Key != "ctrl+n" {
		t.Fatalf("FoundationActions() = %#v", actions)
	}
}
F
function

FoundationHTTPHandlers

examples/quickstart/zz_foundation.gen.go:13-17
func FoundationHTTPHandlers() []foundationweb.HandlerDefinition

{
	return []foundationweb.HandlerDefinition{
		{Method: "GET", Path: "/users/{id:int}", New: func() foundationweb.Handler { return &GetUser{} }},
	}
}
F
function

FoundationActions

examples/quickstart/zz_foundation.gen.go:19-23
func FoundationActions() []foundationactions.Definition

{
	return []foundationactions.Definition{
		{Name: "users.create", Key: "ctrl+n", New: func() foundationactions.Handler { return &CreateUser{} }},
	}
}
F
function

RegisterFoundation

Parameters

application
examples/quickstart/zz_foundation.gen.go:25-28
func RegisterFoundation(application *foundationapp.App)

{
	application.RegisterHTTPDefinitions(FoundationHTTPHandlers()...)
	application.RegisterActionDefinitions(FoundationActions()...)
}