forked from gotnospirit/messageformat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnumber.go
More file actions
50 lines (38 loc) · 1004 Bytes
/
number.go
File metadata and controls
50 lines (38 loc) · 1004 Bytes
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
package messageformat
import (
"bytes"
"errors"
"fmt"
"golang.org/x/text/message"
)
type NumberExpr struct {
Name string
}
func (p *parser) parseNumber(varName string, char rune, start, end int, ptr_input *[]rune) (Expression, int, error) {
var result = NumberExpr{
Name: varName,
}
pos := start
if char != CloseChar {
format, _, cursor, err := readVar(pos+1, end, ptr_input)
if err != nil {
return nil, pos, errors.New("failed to parse number format")
}
pos = cursor
return nil, pos, fmt.Errorf("number format not implemented: %s", format)
}
return &result, pos, nil
}
func (f *formatter) formatNumber(expr Expression, ptrOutput *bytes.Buffer, data map[string]any) error {
e, ok := expr.(*NumberExpr)
if !ok {
return fmt.Errorf("InvalidExprType: want NumberExpr, got %T", e)
}
n, ok := data[e.Name]
if !ok {
return fmt.Errorf("InvalidArgType: want number, got %T", n)
}
p := message.NewPrinter(f.locale)
ptrOutput.WriteString(p.Sprint(n))
return nil
}