package integrationsimport (am)// Kind enumtypeKindenum.Member[string]var (KindReqGetter = Kind{"am_req_getter"}KindReqMutation = Kind{"am_req_mutation"}KindReqWaiting = Kind{"am_req_waiting"}KindRespGetter = Kind{"am_resp_getter"}KindRespMutation = Kind{"am_resp_mutation"}KindRespWaiting = Kind{"am_resp_waiting"}KindEnum = enum.New(KindReqGetter, KindReqMutation, KindReqWaiting,KindRespGetter, KindRespMutation, KindRespWaiting))// flatten to a string in JSONfunc ( *Kind) () ([]byte, error) {returnjson.Marshal(.Value)}func ( *Kind) ( []byte) error {varstring := json.Unmarshal(, &)if != nil {return }// success .Value = returnnil}// TODO validate JSON also for Req and Resp structs// MsgKindReq is a decoding helper.typeMsgKindReqstruct {// The kind of the request. Kind Kind`json:"kind" jsonschema:"required,enum=am_req_getter,enum=am_req_mutation,enum=am_req_waiting"`}// MsgKindResp is a decoding helper.typeMsgKindRespstruct {// The kind of the response. Kind Kind`json:"kind" jsonschema:"required,enum=am_resp_waiting,enum=am_resp_mutation,enum=am_resp_getter"`}// MUTATIONtypeMutationReqstruct {// The kind of the request. Kind Kind`json:"kind" jsonschema:"required,enum=am_req_mutation"`// The states to add to the state machine. Add am.S`json:"add,omitempty" jsonschema:"oneof_required=add"`// The states to remove from the state machine. Remove am.S`json:"remove,omitempty" jsonschema:"oneof_required=remove"`// Arguments passed to transition handlers. Args map[string]any`json:"args,omitempty"`// TODO machine filters to narrow down the request on group channels}typeMutationRespstruct {// The kind of the request. Kind Kind`json:"kind" jsonschema:"required,enum=am_req_mutation"`// The result of the mutation request. Result am.Result`json:"result"`}// WAITINGtypeWaitingReqstruct {// The kind of the request. Kind Kind`json:"kind" jsonschema:"required,enum=am_req_waiting"`// The states to wait for, the default is to all states being active simultaneously (if no time passed). States am.S`json:"states,omitempty" jsonschema:"oneof_required=states"`// The states names to wait for to be inactive. Ignores the Time field. StatesNot am.S`json:"states_not,omitempty" jsonschema:"oneof_required=statesNot"`// The specific (minimal) time to wait for. Time am.Time`json:"time,omitempty"`// TODO WhenArgs}typeWaitingRespUnsafestruct {// The kind of the response. Kind Kind`json:"kind" jsonschema:"required,enum=am_resp_waiting"`// The ID of the state machine. MachId string`json:"mach_id"`// The active states waited for. If time is empty, all these states are active simultaneously. States am.S`json:"states,omitempty" jsonschema:"oneof_required=states"`// The inactive states waited for. StatesNot am.S`json:"states_not,omitempty" jsonschema:"oneof_required=states"`// The requested machine time (the current one may be higher). Time am.Time`json:"time,omitempty"`}typeWaitingRespstruct {WaitingRespUnsafe}func ( *WaitingResp) ( []byte) error { := WaitingRespUnsafe{}if := json.Unmarshal(, &); != nil {return }if .Kind != KindRespWaiting {returnerrors.New("wrong response kind") } .WaitingRespUnsafe = // TODO more validationreturnnil}// GETTER// GetterReq is a generic request, which results in GetterResp with// respective fields filled out.typeGetterReqstruct {// The kind of the request. Kind Kind`json:"kind" jsonschema:"required,enum=am_req_getter"`// Request ticks of the passed states Time am.S`json:"time,omitempty"`// Request the sum of ticks of the passed states TimeSum am.S`json:"time_sum,omitempty"`// Request named clocks of the passed states Clocks am.S`json:"clocks,omitempty"`// Request the tags of the state machine Tags bool`json:"tags,omitempty"`// Request an importable version of the state machine Export bool`json:"export,omitempty"`// Request the ID of the state machine Id bool`json:"id,omitempty"`// Request the ID of the parent state machine ParentId bool`json:"parent_id,omitempty"`}// GetterResp is a response to GetterReq.typeGetterRespstruct {// The kind of the response. Kind Kind`json:"kind" jsonschema:"required,enum=am_resp_getter"`// The ID of the state machine. MachId string`json:"mach_id,omitempty"`// The ticks of the passed states Time am.Time`json:"time,omitempty"`// The sum of ticks of the passed states TimeSum int`json:"time_sum,omitempty"`// The named clocks of the passed states Clocks am.Clock`json:"clocks,omitempty"`// The tags of the state machine Tags []string`json:"tags,omitempty"`// The importable version of the state machine Export *am.Serialized`json:"export,omitempty"`// The ID of the state machine Id string`json:"id,omitempty"`// The ID of the parent state machine ParentId string`json:"parent_id,omitempty"`}// UTILS & HANDLERS// NewGetterReq creates a new getter request.func () *GetterReq {return &GetterReq{Kind: KindReqGetter, }}// NewMutationReq creates a new mutation request.// TODO sugar for NewAddReq and NewRemoveReqfunc () *MutationReq {return &MutationReq{Kind: KindReqMutation, }}// NewWaitingReq creates a new waiting request.func () *WaitingReq {return &WaitingReq{Kind: KindReqWaiting, }}func (context.Context, am.Api, *WaitingReq,) (*WaitingResp, error) { := &WaitingResp{WaitingRespUnsafe{Kind: KindRespWaiting}}// validate := len(.Time) := max(len(.States), len(.StatesNot))if == 0 {returnnil, errors.New("waiting states missing") } elseif > 0 && != len(.States) {returnnil, errors.New("waiting states and time length mismatch") }if !.Has(.States) {returnnil, fmt.Errorf("%w: (%s) for %s", am.ErrStateMissing, .States, .Id()) }// subscribevar <-chanstruct{}iflen(.StatesNot) > 0 { = .WhenNot(.StatesNot, ) .StatesNot = .StatesNot } elseif > 0 { = .WhenTime(.States, .Time, ) } else { = .When(.States, ) .States = .States }// waitselect {case<-.Done():returnnil, .Err()// passcase<-: }// update the response and return .MachId = .Id()if > 0 { .Time = .Time(.States) }return , nil}func (context.Context, am.Api, *MutationReq,) (*MutationResp, error) { := &MutationResp{Kind: KindRespMutation}iflen(.Add) > 0 && len(.Remove) == 0 {if !.Has(.Add) {returnnil, fmt.Errorf("%w: (%s) for %s", am.ErrStateMissing, .Add, .Id()) } .Result = .Add(.Add, .Args) } elseiflen(.Remove) > 0 && len(.Add) == 0 {if !.Has(.Remove) {returnnil, fmt.Errorf("%w: (%s) for %s", am.ErrStateMissing, .Remove, .Id()) } .Result = .Remove(.Remove, .Args) } elseiflen(.Add) > 0 && len(.Remove) > 0 {returnnil, errors.New("mutation can be add or remove, not both") } else {returnnil, errors.New("mutation state names missing") }return , nil}func (context.Context, am.Api, *GetterReq,) (*GetterResp, error) { := &GetterResp{Kind: KindRespGetter}if .Id { .Id = .Id() }if .ParentId { .ParentId = .ParentId() }if .Tags { .Tags = .Tags() }if .Export { .Export, _, _ = .Export() }iflen(.Time) > 0 { .Time = .Time(.Time) }iflen(.Clocks) > 0 { .Clocks = .Clock(.Clocks) }iflen(.TimeSum) > 0 { .TimeSum = int(.Time(.TimeSum).Sum(nil)) }return , nil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.