genfixture

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	CodeLedgerInsufficientFunds = "LEDGER_INSUFFICIENT_FUNDS"
	CodeLedgerEntryNotFound     = "LEDGER_ENTRY_NOT_FOUND"
	CodeLedgerDuplicateID       = "LEDGER_DUPLICATE_ID"
)

错误码常量:错误身份 = 错误码,判定用 apperr.Is(err, Code...), 单体与微服务两种部署形态下行为一致。

View Source
const TopicEntryPosted = "ledger.entry.posted"

TopicEntryPosted 是 EntryPosted 事件的 topic。

View Source
const TopicEntryReversed = "ledger.entry.reversed"

TopicEntryReversed 是 EntryReversed 事件的 topic。

Variables

View Source
var (
	ErrLedgerInsufficientFunds = apperr.New(CodeLedgerInsufficientFunds, 422, "insufficient funds")
	ErrLedgerEntryNotFound     = apperr.New(CodeLedgerEntryNotFound, 404, "entry not found")
	ErrLedgerDuplicateID       = apperr.New(CodeLedgerDuplicateID, 409, "duplicate entry id")
)

错误 sentinel:不可变模板,可安全共享;附加上下文用 WithDetail/WithCause。

Functions

func NewHTTPHandler

func NewHTTPHandler(svc Service) http.Handler

NewHTTPHandler 把 Service 暴露为 HTTP:每方法一条 POST 路由,请求体 JSON decode,错误经 apperr.WriteProblem,成功 200 JSON。与 client.gen.go 由同一份 contract.yaml 生成,编解码约定只有一份。装配形态:

mux.Handle("/", genfixture.NewHTTPHandler(genfixture.WrapService(impl, 0)))

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client 是 greet 契约的远程绑定:与进程内 wrapper(wrap.gen.go)实现 同一个 Service 接口,方法体同样经 contract.Call——事务守卫、ctx 防火墙、 超时与错误规范化在两种部署形态下完全一致(DESIGN §5.3)。

func NewClient

func NewClient(base, caller string, hc *http.Client) *Client

NewClient 返回契约 client。base 是服务根地址(如 "http://ledger:8080"), caller 填本服务名(callctx 白名单的归因值)。

白名单传播焊死在装配处:hc 为 nil 走默认 Transport;非 nil 时在其 Transport 外包一层 callctx.Transport(不改调用方的 client)。 「忘了装 Transport」这个静默失效形态在生成 client 里不存在。

func (*Client) Greet

func (c *Client) Greet(ctx context.Context, req GreetRequest) (GreetReply, error)

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

func (*Client) Reset

func (c *Client) Reset(ctx context.Context, req ResetRequest) error

func (*Client) Search

func (c *Client) Search(ctx context.Context, req SearchRequest) (SearchReply, error)

func (*Client) Stats

func (c *Client) Stats(ctx context.Context) (StatsReply, error)

type Entry

type Entry struct {
	// EntryID 必填。
	EntryID string `json:"entry_id"`
	// Amount 是十进制数字符串:用 money.Parse 解析,全系统禁 float 金额。
	Amount string    `json:"amount"`
	At     time.Time `json:"at"`
}

Entry 是分录概要(List 类响应的元素类型;命名 DTO 嵌套封顶一层)。

type EntryPosted

type EntryPosted struct {
	EntryID string `json:"entry_id"`
	// Amount 是十进制数字符串:用 money.Parse 解析,全系统禁 float 金额。
	Amount   string    `json:"amount"`
	At       time.Time `json:"at"`
	Attempts int64     `json:"attempts"`
	Final    bool      `json:"final"`
}

EntryPosted 事件 payload(topic: ledger.entry.posted)。

func ParseEntryPosted

func ParseEntryPosted(evt appkit.Event) (EntryPosted, error)

ParseEntryPosted 从 appkit.Event 还原 EntryPosted:校验 topic 与必填字段。

func (EntryPosted) Event

func (e EntryPosted) Event() (appkit.Event, error)

Event 把事件序列化为 appkit.Event。ID 留空,由 outbox 发布时填充。

type EntryReversed

type EntryReversed struct {
	EntryID string `json:"entry_id"`
	Reason  string `json:"reason"`
}

EntryReversed 事件 payload(topic: ledger.entry.reversed)。

func ParseEntryReversed

func ParseEntryReversed(evt appkit.Event) (EntryReversed, error)

ParseEntryReversed 从 appkit.Event 还原 EntryReversed:校验 topic 与必填字段。

func (EntryReversed) Event

func (e EntryReversed) Event() (appkit.Event, error)

Event 把事件序列化为 appkit.Event。ID 留空,由 outbox 发布时填充。

type GreetReply

type GreetReply struct {
	Message string `json:"message"`
}

GreetReply 是 Greet 的响应 DTO。

type GreetRequest

type GreetRequest struct {
	// Name 必填。
	Name string `json:"name"`
}

GreetRequest 是 Greet 的请求 DTO:契约边界传值、可序列化。

type ResetRequest

type ResetRequest struct {
	Scope string `json:"scope"`
}

ResetRequest 是 Reset 的请求 DTO:契约边界传值、可序列化。

type SearchReply

type SearchReply struct {
	Entries    []Entry `json:"entries"`
	NextCursor string  `json:"next_cursor"`
}

SearchReply 是 Search 的响应 DTO。

type SearchRequest

type SearchRequest struct {
	Prefix string `json:"prefix"`
}

SearchRequest 是 Search 的请求 DTO:契约边界传值、可序列化。

type Service

type Service interface {
	// Greet 打招呼(req + resp 形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
	Greet(ctx context.Context, req GreetRequest) (GreetReply, error)
	// Stats 服务统计(无 req、有 resp 形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
	Stats(ctx context.Context) (StatsReply, error)
	// Reset 重置(有 req、仅 error 形态)。
	Reset(ctx context.Context, req ResetRequest) error
	// Ping 探活(仅 ctx、仅 error 形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
	Ping(ctx context.Context) error
	// Search 检索分录(命名 DTO 数组响应形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
	Search(ctx context.Context, req SearchRequest) (SearchReply, error)
}

Service 是 greet 域的契约接口(事实源 contract.yaml)。进程内 wrapper 与远程 client 实现同一个接口,调用方无从(也不需要)分辨对端形态。

func WrapService

func WrapService(inner Service, timeout time.Duration) Service

WrapService 把实现包进契约边界,供 appkit.ProvideContract 使用; timeout <= 0 时使用 contract.DefaultTimeout。

type StatsReply

type StatsReply struct {
	Served int64 `json:"served"`
}

StatsReply 是 Stats 的响应 DTO。

Jump to

Keyboard shortcuts

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