-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathselect.go
133 lines (105 loc) · 2.84 KB
/
select.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package messageformat
import (
"bytes"
"errors"
)
type selectExpr struct {
key string
choices map[string]*node
}
func parseSelect(varname string, ptr_compiler *Parser, char rune, start, end int, ptr_input *[]rune) (Expression, int, error) {
result := new(selectExpr)
result.key = varname
result.choices = make(map[string]*node)
if char != PartChar {
return nil, start, errors.New("MalformedOption")
}
hasOtherChoice := false
pos := start + 1
for pos < end {
key, char, i, err := readKey(pos, end, ptr_input)
if err != nil {
return nil, i, err
} else if char == ':' {
return nil, i, errors.New("UnexpectedExtension")
}
if key == "other" {
hasOtherChoice = true
}
choice, char, i, err := readChoice(ptr_compiler, char, i, end, ptr_input)
if err != nil {
return nil, i, err
}
result.choices[key] = choice
pos = i
if char == CloseChar {
break
}
}
if !hasOtherChoice {
return nil, pos, errors.New("MissingMandatoryChoice")
}
return result, pos, nil
}
// formatSelect is the format function associated with the "select" type.
//
// It will falls back to the "other" choice if :
// - its key can't be found in the given map
// - its string representation is not a key of the given map
//
// It will returns an error if :
// - the associated value can't be convert to string (i.e. struct {}, ...)
func formatSelect(expr Expression, ptr_output *bytes.Buffer, data *map[string]interface{}, ptr_mf *MessageFormat, _ string) error {
o := expr.(*selectExpr)
value, err := toString(*data, o.key)
if err != nil {
return err
}
choice, ok := o.choices[value]
if !ok {
choice = o.choices["other"]
}
return choice.format(ptr_output, data, ptr_mf, value)
}
func readKey(start, end int, ptr_input *[]rune) (string, rune, int, error) {
char, pos := whitespace(start, end, ptr_input)
fc_pos, lc_pos := pos, pos
input := *ptr_input
for pos < end {
switch char {
default:
lc_pos = pos + 1
case ' ', '\r', '\n', '\t':
char, pos = whitespace(pos+1, end, ptr_input)
return string(input[fc_pos:lc_pos]), char, pos, nil
case ':', PartChar, CloseChar, OpenChar:
if fc_pos != lc_pos {
return string(input[fc_pos:lc_pos]), char, pos, nil
}
return "", char, pos, errors.New("MissingChoiceName")
}
pos++
if pos < end {
char = input[pos]
}
}
return "", char, pos, errors.New("UnbalancedBraces")
}
func readChoice(ptr_compiler *Parser, char rune, pos, end int, ptr_input *[]rune) (*node, rune, int, error) {
if char != OpenChar {
return nil, char, pos, errors.New("MissingChoiceContent")
}
choice := new(node)
pos, _, err := ptr_compiler.parse(pos+1, end, ptr_input, choice)
if err != nil {
return nil, char, pos, err
}
pos++
if pos < end {
char = (*ptr_input)[pos]
}
if isWhitespace(char) {
char, pos = whitespace(pos+1, end, ptr_input)
}
return choice, char, pos, nil
}