Skip to content

Commit 7a69050

Browse files
daniel-hutaoKeHaohaoke
authored andcommitted
refactor: unify the statekey getting method
Signed-off-by: Daniel Hu <tao.hu@merico.dev>
1 parent bcdf2bc commit 7a69050

File tree

12 files changed

+31
-36
lines changed

12 files changed

+31
-36
lines changed

internal/pkg/configmanager/configmanager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *Config) ValidateDependency() []error {
4444
toolMap := make(map[string]bool)
4545
// creating the set
4646
for _, tool := range c.Tools {
47-
toolMap[tool.Key()] = true
47+
toolMap[tool.KeyWithNameAndInstanceID()] = true
4848
}
4949

5050
for _, tool := range c.Tools {

internal/pkg/configmanager/toolconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (t *Tool) DeepCopy() *Tool {
4040
return &retTool
4141
}
4242

43-
func (t *Tool) Key() string {
43+
func (t *Tool) KeyWithNameAndInstanceID() string {
4444
return fmt.Sprintf("%s.%s", t.Name, t.InstanceID)
4545
}
4646

internal/pkg/configmanager/toolconfig_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ var _ = Describe("Tool Config", func() {
2424
})
2525
})
2626

27-
Context("Key method", func() {
27+
Context("KeyWithNameAndInstanceID method", func() {
2828
It("should return joined key", func() {
2929
toolName := "test_tool"
3030
toolInstance := "0"
3131
tool := configmanager.Tool{Name: toolName, InstanceID: toolInstance}
32-
key := tool.Key()
32+
key := tool.KeyWithNameAndInstanceID()
3333
Expect(key).Should(Equal(fmt.Sprintf("%s.%s", toolName, toolInstance)))
3434
})
3535
})

internal/pkg/plugin/jiragithub/templates.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package jiragithub
22

3-
var jiraIssuesBuilder = `name: jira-github-integ Builder
3+
var jiraIssuesBuilder = `name: jira-github-integ Builder
44
on:
55
issues:
66
types: [opened, reopened, edited, closed]
@@ -28,7 +28,7 @@ jobs:
2828
issuetype: Task
2929
summary: ${{ github.event.issue.title }}
3030
description: ${{ github.event.issue.body }}
31-
31+
3232
- name: Rename github issue
3333
if: ${{ github.event.action == 'opened' }}
3434
uses: actions-cool/issues-helper@v3
@@ -40,14 +40,14 @@ jobs:
4040
title: "[${{ steps.create.outputs.issue }}] ${{ github.event.issue.title }}"
4141
update-mode: 'replace'
4242
emoji: '+1'
43-
43+
4444
- name: Find Jira Issue Key
4545
id: find
4646
if: ${{ github.event.action != 'opened' }}
4747
uses: atlassian/gajira-find-issue-key@master
4848
with:
4949
string: "${{ github.event.issue.title }}"
50-
50+
5151
- name: Transition issue to In Progress
5252
id: transition_inprogress
5353
if: ${{ github.event.action == 'edited' }}
@@ -71,7 +71,7 @@ jobs:
7171
with:
7272
issue: ${{ steps.find.outputs.issue }}
7373
transition: "To Do"
74-
74+
7575
issue_comment_integration:
7676
if: ${{ github.event_name == 'issue_comment' }}
7777
runs-on: ubuntu-latest
@@ -87,7 +87,7 @@ jobs:
8787
id: find
8888
uses: atlassian/gajira-find-issue-key@master
8989
with:
90-
string: "${{ github.event.issue.title }}"
90+
string: "${{ github.event.issue.title }}"
9191
- name: Create issue
9292
id: create_issue
9393
if: ${{ github.event.action == 'created' }}
@@ -103,7 +103,7 @@ jobs:
103103
with:
104104
issue: ${{ steps.find.outputs.issue }}
105105
comment: "updated: ${{ github.event.comment.body }} from: ${{ github.event.changes.body.from }}"
106-
106+
107107
- name: Delete issue
108108
id: del_issue
109109
if: ${{ github.event.action == 'deleted' }}

internal/pkg/pluginengine/change.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func handleResult(smgr statemanager.Manager, change *Change) error {
143143
}
144144

145145
if change.ActionName == statemanager.ActionDelete {
146-
key := statemanager.StateKeyGenerateFunc(change.Tool)
146+
key := statemanager.GenerateStateKeyByToolNameAndInstanceID(change.Tool.Name, change.Tool.InstanceID)
147147
log.Infof("Prepare to delete '%s' from States.", key)
148148
err := smgr.DeleteState(key)
149149
if err != nil {
@@ -154,7 +154,7 @@ func handleResult(smgr statemanager.Manager, change *Change) error {
154154
return nil
155155
}
156156

157-
key := statemanager.StateKeyGenerateFunc(change.Tool)
157+
key := statemanager.GenerateStateKeyByToolNameAndInstanceID(change.Tool.Name, change.Tool.InstanceID)
158158
state := statemanager.State{
159159
Name: change.Tool.Name,
160160
InstanceID: change.Tool.InstanceID,

internal/pkg/pluginengine/change_helper.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func GetChangesForApply(smgr statemanager.Manager, cfg *configmanager.Config) (c
9797
// 3. generate changes for each tool
9898
for _, batch := range batchesOfTools {
9999
for _, tool := range batch {
100-
state := smgr.GetState(statemanager.StateKeyGenerateFunc(&tool))
100+
state := smgr.GetState(statemanager.GenerateStateKeyByToolNameAndInstanceID(tool.Name, tool.InstanceID))
101101

102102
if state == nil {
103103
// tool not in the state, create, no need to Read resource before Create
@@ -142,7 +142,7 @@ func GetChangesForApply(smgr statemanager.Manager, cfg *configmanager.Config) (c
142142
}
143143

144144
// delete the tool from the temporary state map since it's already been processed above
145-
tmpStatesMap.Delete(statemanager.StateKeyGenerateFunc(&tool))
145+
tmpStatesMap.Delete(statemanager.GenerateStateKeyByToolNameAndInstanceID(tool.Name, tool.InstanceID))
146146
}
147147
}
148148

@@ -177,7 +177,7 @@ func GetChangesForDelete(smgr statemanager.Manager, cfg *configmanager.Config, i
177177
batch := batchesOfTools[i]
178178
for _, tool := range batch {
179179
if !isForceDelete {
180-
state := smgr.GetState(statemanager.StateKeyGenerateFunc(&tool))
180+
state := smgr.GetState(statemanager.GenerateStateKeyByToolNameAndInstanceID(tool.Name, tool.InstanceID))
181181
if state == nil {
182182
continue
183183
}
@@ -220,7 +220,7 @@ func GetChangesForDestroy(smgr statemanager.Manager, isForceDestroy bool) (chang
220220
batch := batchesOfTools[i]
221221
for _, tool := range batch {
222222
if !isForceDestroy {
223-
state := smgr.GetState(statemanager.StateKeyGenerateFunc(&tool))
223+
state := smgr.GetState(statemanager.GenerateStateKeyByToolNameAndInstanceID(tool.Name, tool.InstanceID))
224224
if state == nil {
225225
continue
226226
}
@@ -264,7 +264,7 @@ func topologicalSortChangesInBatch(changes []*Change) ([][]*Change, error) {
264264
// for each tool in the batch, find the change that matches it
265265
for _, tool := range batch {
266266
// only add the change that has the tool match with it
267-
if change, ok := changesKeyMap[tool.Key()]; ok {
267+
if change, ok := changesKeyMap[tool.KeyWithNameAndInstanceID()]; ok {
268268
changesOneBatch = append(changesOneBatch, change)
269269
}
270270
}
@@ -285,9 +285,9 @@ func getToolsFromChanges(changes []*Change) []configmanager.Tool {
285285

286286
// get tools from changes avoiding duplicated tools
287287
for _, change := range changes {
288-
if _, ok := toolsKeyMap[change.Tool.Key()]; !ok {
288+
if _, ok := toolsKeyMap[change.Tool.KeyWithNameAndInstanceID()]; !ok {
289289
tools = append(tools, *change.Tool)
290-
toolsKeyMap[change.Tool.Key()] = struct{}{}
290+
toolsKeyMap[change.Tool.KeyWithNameAndInstanceID()] = struct{}{}
291291
}
292292
}
293293

@@ -297,7 +297,7 @@ func getToolsFromChanges(changes []*Change) []configmanager.Tool {
297297
func getChangesKeyMap(changes []*Change) map[string]*Change {
298298
changesKeyMap := make(map[string]*Change)
299299
for _, change := range changes {
300-
changesKeyMap[change.Tool.Key()] = change
300+
changesKeyMap[change.Tool.KeyWithNameAndInstanceID()] = change
301301
}
302302
return changesKeyMap
303303
}

internal/pkg/pluginengine/outputs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func HandleOutputsReferences(smgr statemanager.Manager, options map[string]inter
2222
continue
2323
}
2424

25-
outputs, err := smgr.GetOutputs(statemanager.GenerateStateKeyByToolNameAndPluginKind(toolName, instanceID))
25+
outputs, err := smgr.GetOutputs(statemanager.GenerateStateKeyByToolNameAndInstanceID(toolName, instanceID))
2626
if err != nil {
2727
errorsList = append(errorsList, err)
2828
continue

internal/pkg/pluginengine/topological_sort.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func topologicalSort(tools []configmanager.Tool) ([][]configmanager.Tool, error)
3232
// a "graph", which contains "nodes" that haven't been processed yet
3333
unprocessedNodeSet := make(map[string]bool)
3434
for _, tool := range tools {
35-
unprocessedNodeSet[tool.Key()] = true
35+
unprocessedNodeSet[tool.KeyWithNameAndInstanceID()] = true
3636
}
3737

3838
// while there is still a node in the graph left to be processed:
@@ -42,7 +42,7 @@ func topologicalSort(tools []configmanager.Tool) ([][]configmanager.Tool, error)
4242

4343
for _, tool := range tools {
4444
// if the tool has already been processed (not in the unprocessedNodeSet anymore), pass
45-
if _, ok := unprocessedNodeSet[tool.Key()]; !ok {
45+
if _, ok := unprocessedNodeSet[tool.KeyWithNameAndInstanceID()]; !ok {
4646
continue
4747
}
4848

@@ -68,7 +68,7 @@ func topologicalSort(tools []configmanager.Tool) ([][]configmanager.Tool, error)
6868

6969
// remove tools from the unprocessedNodeSet because they have been added to the batch
7070
for _, tool := range batch {
71-
delete(unprocessedNodeSet, tool.Key())
71+
delete(unprocessedNodeSet, tool.KeyWithNameAndInstanceID())
7272
}
7373

7474
// add the batch to the final result

internal/pkg/show/status/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func showAll(smgr statemanager.Manager) error {
8484
func showOne(smgr statemanager.Manager, id, plugin string) error {
8585
// get state from statemanager
8686
state := smgr.GetState(
87-
statemanager.GenerateStateKeyByToolNameAndPluginKind(plugin, id),
87+
statemanager.GenerateStateKeyByToolNameAndInstanceID(plugin, id),
8888
)
8989
if state == nil {
9090
return fmt.Errorf("state with (id: %s, plugin: %s) not found", id, plugin)

internal/pkg/statemanager/state.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"gopkg.in/yaml.v3"
99

10-
"github.com/devstream-io/devstream/internal/pkg/configmanager"
1110
"github.com/devstream-io/devstream/pkg/util/log"
1211
"github.com/devstream-io/devstream/pkg/util/mapz/concurrentmap"
1312
)
@@ -85,13 +84,9 @@ func (s StatesMap) Format() []byte {
8584
return buf.Bytes()
8685
}
8786

88-
// Note: Please use the StateKeyGenerateFunc function to generate StateKey instance.
87+
// Note: Please use the GenerateStateKeyByToolNameAndInstanceID function to generate StateKey instance.
8988
type StateKey string
9089

91-
func StateKeyGenerateFunc(t *configmanager.Tool) StateKey {
92-
return StateKey(fmt.Sprintf("%s_%s", t.Name, t.InstanceID))
93-
}
94-
95-
func GenerateStateKeyByToolNameAndPluginKind(toolName string, instanceID string) StateKey {
90+
func GenerateStateKeyByToolNameAndInstanceID(toolName string, instanceID string) StateKey {
9691
return StateKey(fmt.Sprintf("%s_%s", toolName, instanceID))
9792
}

0 commit comments

Comments
 (0)