-
Notifications
You must be signed in to change notification settings - Fork 0
/
steps.go
94 lines (81 loc) · 1.69 KB
/
steps.go
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
package auth
import (
authv1 "github.com/harmony-development/legato/gen/auth/v1"
"github.com/samber/lo"
)
type AuthStep struct {
ID string
CanGoBack bool
PreviousStep string
}
type FormField struct {
Name string
Type string
}
type FormStep struct {
AuthStep
Fields []FormField
}
func NewField(name, type_ string) FormField {
return FormField{
Name: name,
Type: type_,
}
}
type ChoiceStep struct {
AuthStep
Choices []string
}
func (s ChoiceStep) StepInfo() AuthStep {
return s.AuthStep
}
func (s FormStep) StepInfo() AuthStep {
return s.AuthStep
}
func (s ChoiceStep) ToProtoV1() *authv1.AuthStep {
return &authv1.AuthStep{
CanGoBack: s.CanGoBack,
Step: &authv1.AuthStep_Choice_{
Choice: &authv1.AuthStep_Choice{
Title: s.ID,
Options: s.Choices,
},
},
}
}
func (s FormStep) ToProtoV1() *authv1.AuthStep {
return &authv1.AuthStep{
CanGoBack: s.CanGoBack,
Step: &authv1.AuthStep_Form_{
Form: &authv1.AuthStep_Form{
Title: s.ID,
Fields: lo.Map(s.Fields, func(field FormField, _ int) *authv1.AuthStep_Form_FormField {
return &authv1.AuthStep_Form_FormField{
Name: field.Name,
Type: field.Type,
}
}),
},
},
}
}
func newAuthStep(id string, previousStep string) AuthStep {
s := AuthStep{ID: id}
if previousStep != "" {
s.CanGoBack = true
s.PreviousStep = previousStep
}
return s
}
func NewForm(id string, fields []FormField, previousStep string) FormStep {
return FormStep{
AuthStep: newAuthStep(id, previousStep),
Fields: fields,
}
}
func NewChoice(id string, choices []string, previousStep string) ChoiceStep {
return ChoiceStep{
AuthStep: newAuthStep(id, previousStep),
Choices: choices,
}
}