-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
if.go
54 lines (48 loc) · 1.27 KB
/
if.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
package op
import (
"fmt"
"reflect"
"github.com/emicklei/melrose/core"
)
type IfCondition struct {
Condition core.HasValue
Then core.Sequenceable
Else core.Sequenceable
}
func (i IfCondition) S() core.Sequence {
b, ok := i.Condition.Value().(bool)
if !ok {
return i.Else.S()
}
if !b {
return i.Else.S()
}
return i.Then.S()
}
func (i IfCondition) Storex() string {
if reflect.DeepEqual(i.Else, core.EmptySequence) {
return fmt.Sprintf("if(%s,%s)", core.Storex(i.Condition), core.Storex(i.Then))
}
return fmt.Sprintf("if(%s,%s,%s)", core.Storex(i.Condition), core.Storex(i.Then), core.Storex(i.Else))
}
// Replaced is part of Replaceable
func (i IfCondition) Replaced(from, to core.Sequenceable) core.Sequenceable {
if core.IsIdenticalTo(i, from) {
return to
}
if core.IsIdenticalTo(i.Then, from) {
return IfCondition{Condition: i.Condition, Then: to, Else: i.Else}
}
if core.IsIdenticalTo(i.Else, from) {
return IfCondition{Condition: i.Condition, Then: i.Then, Else: to}
}
repThen := i.Then
if r, ok := repThen.(core.Replaceable); ok {
repThen = r.Replaced(from, to)
}
repElse := i.Else
if r, ok := repElse.(core.Replaceable); ok {
repElse = r.Replaced(from, to)
}
return IfCondition{Condition: i.Condition, Then: repThen, Else: repElse}
}