Skip to content

Commit

Permalink
Merge pull request #666 from k1LoW/refactor-runbook-id
Browse files Browse the repository at this point in the history
Refactor generating runbook ID
  • Loading branch information
k1LoW authored Oct 31, 2023
2 parents 4f85b8a + 947e19e commit 1f491d7
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 148 deletions.
35 changes: 3 additions & 32 deletions operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"
"time"

"github.com/goccy/go-json"
"github.com/k1LoW/concgroup"
"github.com/k1LoW/stopw"
"github.com/ryo-yamaoka/otchkiss"
Expand Down Expand Up @@ -81,39 +80,11 @@ func (o *operator) ID() string {

// runbookID returns id of the root runbook.
func (o *operator) runbookID() string {
trs := o.trails()
var id string
L:
for _, tr := range trs {
switch tr.Type {
case TrailTypeRunbook:
id = tr.RunbookID
break L
}
}
return id
return o.trails().runbookID()
}

func (o *operator) runbookIDFull() string { //nolint:unused
trs := o.trails()
var (
id string
steps []string
)
for _, tr := range trs {
switch tr.Type {
case TrailTypeRunbook:
if id == "" {
id = tr.RunbookID
}
case TrailTypeStep:
steps = append(steps, fmt.Sprintf("step=%d", *tr.StepIndex))
}
}
if len(steps) == 0 {
return id
}
return fmt.Sprintf("%s?%s", id, strings.Join(steps, "&"))
return o.trails().runbookIDFull()
}

// Desc returns `desc:` of runbook.
Expand Down Expand Up @@ -1266,7 +1237,7 @@ func (ops *operators) DumpProfile(w io.Writer) error {
if r == nil {
return errors.New("no profile")
}
enc := json.NewEncoder(w)
enc := ejson.NewEncoder(w)
if err := enc.Encode(r); err != nil {
return err
}
Expand Down
41 changes: 0 additions & 41 deletions operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1181,47 +1181,6 @@ func TestTrails(t *testing.T) {
}
}

func TestRunbookID(t *testing.T) {
s2 := 2
s3 := 3

tests := []struct {
o *operator
want string
wantFull string
}{
{
&operator{id: "o-a"},
"o-a",
"o-a",
},
{
&operator{id: "o-a", parent: &step{idx: s2, key: "s-b", parent: &operator{id: "o-c"}}},
"o-c",
"o-c?step=2",
},
{
&operator{id: "o-a", parent: &step{idx: s2, key: "s-b", parent: &operator{id: "o-c", parent: &step{idx: s3, key: "s-d", parent: &operator{id: "o-e"}}}}},
"o-e",
"o-e?step=3&step=2",
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
got := tt.o.runbookID()
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
{
got := tt.o.runbookIDFull()
if got != tt.wantFull {
t.Errorf("got %v\nwant %v", got, tt.wantFull)
}
}
})
}
}

func newRunNResult(t *testing.T, total int64, results []*RunResult) *runNResult {
r := &runNResult{}
r.Total.Store(total)
Expand Down
34 changes: 2 additions & 32 deletions step.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package runn

import (
"errors"
"fmt"
"strings"
)

type step struct {
Expand Down Expand Up @@ -79,39 +77,11 @@ func (s *step) generateTrail() Trail {

// runbookID returns id of the root runbook.
func (s *step) runbookID() string { //nolint:unused
trs := s.trails()
var id string
L:
for _, tr := range trs {
switch tr.Type {
case TrailTypeRunbook:
id = tr.RunbookID
break L
}
}
return id
return s.trails().runbookID()
}

func (s *step) runbookIDFull() string { //nolint:unused
trs := s.trails()
var (
id string
steps []string
)
for _, tr := range trs {
switch tr.Type {
case TrailTypeRunbook:
if id == "" {
id = tr.RunbookID
}
case TrailTypeStep:
steps = append(steps, fmt.Sprintf("step=%d", *tr.StepIndex))
}
}
if len(steps) == 0 {
return id
}
return fmt.Sprintf("%s?%s", id, strings.Join(steps, "&"))
return s.trails().runbookIDFull()
}

func (s *step) trails() Trails {
Expand Down
42 changes: 0 additions & 42 deletions step_test.go

This file was deleted.

39 changes: 38 additions & 1 deletion trail.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package runn

import "fmt"
import (
"fmt"
"strings"
)

type TrailType string

Expand Down Expand Up @@ -63,3 +66,37 @@ func (trs Trails) toInterfaceSlice() []any { //nostyle:recvtype
}
return s
}

func (trs Trails) runbookID() string {

Check failure on line 70 in trail.go

View workflow job for this annotation

GitHub Actions / Test

[gostyle.recvtype] When in doubt, use a pointer receiver. (GOSTYLE MEMO: It's a strong check, so read Go Style and decide if it should be ignored or not proactively) (ref: https://google.github.io/styleguide/go/decisions#receiver-type ): runbookID
var id string
L:
for _, tr := range trs {
switch tr.Type {
case TrailTypeRunbook:
id = tr.RunbookID
break L
}
}
return id
}

func (trs Trails) runbookIDFull() string {

Check failure on line 83 in trail.go

View workflow job for this annotation

GitHub Actions / Test

[gostyle.recvtype] When in doubt, use a pointer receiver. (GOSTYLE MEMO: It's a strong check, so read Go Style and decide if it should be ignored or not proactively) (ref: https://google.github.io/styleguide/go/decisions#receiver-type ): runbookIDFull
var (
id string
steps []string
)
for _, tr := range trs {
switch tr.Type {
case TrailTypeRunbook:
if id == "" {
id = tr.RunbookID
}
case TrailTypeStep:
steps = append(steps, fmt.Sprintf("step=%d", *tr.StepIndex))
}
}
if len(steps) == 0 {
return id
}
return fmt.Sprintf("%s?%s", id, strings.Join(steps, "&"))
}
89 changes: 89 additions & 0 deletions trail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package runn

import (
"fmt"
"testing"
)

func TestTrailRunbookID(t *testing.T) {
s2 := 2
s3 := 3

tests := []struct {
trails Trails
want string
wantFull string
}{
{
Trails{
Trail{
Type: TrailTypeRunbook,
RunbookID: "o-a",
},
},
"o-a",
"o-a",
},
{
Trails{
Trail{
Type: TrailTypeRunbook,
RunbookID: "o-c",
},
Trail{
Type: TrailTypeStep,
StepIndex: &s2,
StepKey: "s-b",
},
Trail{
Type: TrailTypeRunbook,
RunbookID: "o-a",
},
},
"o-c",
"o-c?step=2",
},
{
Trails{
Trail{
Type: TrailTypeRunbook,
RunbookID: "o-e",
},
Trail{
Type: TrailTypeStep,
StepIndex: &s3,
StepKey: "s-d",
},
Trail{
Type: TrailTypeRunbook,
RunbookID: "o-c",
},
Trail{
Type: TrailTypeStep,
StepIndex: &s2,
StepKey: "s-b",
},
Trail{
Type: TrailTypeRunbook,
RunbookID: "o-a",
},
},
"o-e",
"o-e?step=3&step=2",
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
got := tt.trails.runbookID()
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
{
got := tt.trails.runbookIDFull()
if got != tt.wantFull {
t.Errorf("got %v\nwant %v", got, tt.wantFull)
}
}
})
}
}

0 comments on commit 1f491d7

Please sign in to comment.