forked from gotnospirit/messageformat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathselect.go
More file actions
138 lines (109 loc) · 2.93 KB
/
select.go
File metadata and controls
138 lines (109 loc) · 2.93 KB
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
134
135
136
137
138
package messageformat
import (
"bytes"
"errors"
"fmt"
)
type SelectExpr struct {
Key string `json:"key"`
Choices map[string]*ParseTree `json:"choices"`
}
func (p *parser) parseSelect(varname string, char rune, start, end int, ptr_input *[]rune) (Expression, int, error) {
result := new(SelectExpr)
result.Key = varname
result.Choices = make(map[string]*ParseTree)
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 == ColonChar {
return nil, i, errors.New("UnexpectedExtension")
}
if key == "other" {
hasOtherChoice = true
}
choice, char, i, err := p.readChoice(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 (f *formatter) formatSelect(expr Expression, ptr_output *bytes.Buffer, data map[string]any) error {
o, ok := expr.(*SelectExpr)
if !ok {
return fmt.Errorf("InvalidExprType: want SelectExpr, got %T", expr)
}
value, err := toString(data, o.Key)
if err != nil {
return err
}
choice, ok := o.Choices[value]
if !ok {
choice = o.Choices["other"]
}
return f.format(choice, ptr_output, data, 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 (p *parser) readChoice(char rune, pos, end int, ptr_input *[]rune) (*ParseTree, rune, int, error) {
if OpenChar != char {
return nil, char, pos, errors.New("MissingChoiceContent")
}
choice := new(ParseTree)
pos, _, err := p.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
}