-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvo.go
More file actions
112 lines (85 loc) · 3.56 KB
/
Copy pathconvo.go
File metadata and controls
112 lines (85 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package codegen
import (
"fmt"
"github.com/streamingfast/logging"
"github.com/streamingfast/substreams-codegen/loop"
)
var zlog, tracer = logging.PackageLogger("convo", "github.com/streamingfast/substreams-codegen/codegen")
// ConversationState defines the minimal interface that a conversation state
// must implement to be used within the generic Conversation[X ConversationState] struct.
//
// Embedding [BaseConversationState] is recommended to get default implementations
// of the methods.
type ConversationState interface {
// GetChainName should return the Network Registry chain's id ideally (or alias)
// representing the chain the user selected. It expected to be "" before the actual
// user made a decision
GetChainName() string
// GetChainDisplayName should return the Network Registry chain's full name
// representing the chain the user selected. It expected to be "" before the actual
// user made a decision
GetChainDisplayName() string
// SetChainName should set the chain name in the state.
SetChainName(name string)
// GetModuleName should return the module name to be used in the generated substreams.yaml
// that the user should call in substreams run/gui/sink commands.
GetModuleName() string
// SetProjectName should set the project name in the state, this also derives the module name
// so populated [GetModuleName] will return a non-empty value after this method is called.
SetProjectName(name string)
// GetSubstreamsSinkChoice should return the user's choice of Substreams sink.
GetSubstreamsSinkChoice() *SubstreamsSinkChoice
// SetSubstreamsSinkChoice should set the user's choice of Substreams sink.
SetSubstreamsSinkChoice(choice SubstreamsSinkChoice)
// Generate the ReturnGenerate containing the generated files or error.
Generate() ReturnGenerate
}
type ValidatingConversationState interface {
ConversationState
Validate() error
}
type Conversation[X ConversationState] struct {
// State holds the current conversation state, check [BaseConversationState] for common fields
// every state should embedded and fullfil the interface [ConversationState].
State X
clientVersion uint32
factory *MsgWrapFactory
}
func (c *Conversation[X]) SetFactory(f *MsgWrapFactory) {
c.factory = f
}
func (c *Conversation[X]) SetClientVersion(version uint32) {
c.clientVersion = version
}
func (c *Conversation[X]) GetState() ConversationState {
return c.State
}
func (c *Conversation[X]) Msg() *MsgWrap { return c.factory.NewMsg(c.State) }
func (c *Conversation[X]) Action(element any) *MsgWrap {
return c.factory.NewInput(element, c.State)
}
func NewBaseBlockchainGeneratorConversation[X ConversationState](state X, config SharedFlowConfig) *BaseBlockchainGeneratorConversation[X] {
return &BaseBlockchainGeneratorConversation[X]{
Conversation: Conversation[X]{State: state},
sharedFlowConfig: config,
}
}
type BaseBlockchainGeneratorConversation[X ConversationState] struct {
Conversation[X]
sharedFlowConfig SharedFlowConfig
}
func (c *BaseBlockchainGeneratorConversation[X]) NextStep() loop.Cmd {
if !c.IsPreSharedFlowDone(c.sharedFlowConfig) {
return c.NextPreSharedFlowStep(c.sharedFlowConfig)
}
return c.NextPostSharedFlowStep(c.sharedFlowConfig)
}
func (c *BaseBlockchainGeneratorConversation[X]) Update(msg loop.Msg) loop.Cmd {
if c.IsPreSharedFlowMsg(msg, c.sharedFlowConfig) {
return c.UpdatePreSharedFlowMsg(msg, c.sharedFlowConfig, c.NextStep)
}
if c.IsPostSharedFlowMsg(msg, c.sharedFlowConfig) {
return c.UpdatePostSharedFlowMsg(msg, c.sharedFlowConfig)
}
return loop.Quit(fmt.Errorf("invalid loop message: %T", msg))
}