-
Notifications
You must be signed in to change notification settings - Fork 4
/
state.go
65 lines (53 loc) · 1.11 KB
/
state.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
package main
import (
"database/sql/driver"
"encoding/json"
"fmt"
// tele "gopkg.in/telebot.v3"
"strconv"
)
type Step struct {
Field string
Prompt string
Input *string
Next *Step
}
type State struct {
ID *uint
Name string
FirstStep Step
}
// Value implements the driver.Valuer interface, allowing
// for converting the State to a JSON string for database storage.
func (s State) Value() (driver.Value, error) {
if s.ID == nil && s.Name == "" && s.FirstStep == (Step{}) {
return nil, nil
}
return json.Marshal(s)
}
// Scan implements the sql.Scanner interface, allowing for
// converting a JSON string from the database back into the State slice.
func (s *State) Scan(value interface{}) error {
if value == nil {
s = nil
return nil
}
b, ok := value.([]byte)
if !ok {
return fmt.Errorf("type assertion to []byte failed")
}
return json.Unmarshal(b, &s)
}
func findEmptyStep(step *Step) *Step {
if step.Input != nil {
if step.Next == nil {
return nil
}
return findEmptyStep(step.Next)
}
return step
}
func as_uint(s string) uint {
i, _ := strconv.Atoi(s)
return uint(i)
}