...
1
2
3
4
5 package cldrtree
6
7 import (
8 "reflect"
9
10 "golang.org/x/text/unicode/cldr"
11 )
12
13
14 type Option func(*options)
15
16 type options struct {
17 parent *Index
18
19 name string
20 alias *cldr.Common
21
22 sharedType *typeInfo
23 sharedEnums *enum
24 }
25
26 func (o *options) fill(opt []Option) {
27 for _, f := range opt {
28 f(o)
29 }
30 }
31
32
33 func (o *options) setAlias(n Element) {
34 if n != nil && !reflect.ValueOf(n).IsNil() {
35 o.alias = n.GetCommon()
36 }
37 }
38
39
40
41
42
43
44
45 func Enum(name string, value ...string) Option {
46 return EnumFunc(name, nil, value...)
47 }
48
49
50 func EnumFunc(name string, rename func(string) string, value ...string) Option {
51 enum := &enum{name: name, rename: rename, keyMap: map[string]enumIndex{}}
52 for _, e := range value {
53 enum.lookup(e)
54 }
55 return func(o *options) {
56 found := false
57 for _, e := range o.parent.meta.b.enums {
58 if e.name == enum.name {
59 found = true
60 break
61 }
62 }
63 if !found {
64 o.parent.meta.b.enums = append(o.parent.meta.b.enums, enum)
65 }
66 o.sharedEnums = enum
67 }
68 }
69
70
71
72 func SharedType() Option {
73 info := &typeInfo{}
74 return func(o *options) { o.sharedType = info }
75 }
76
77 func useSharedType() Option {
78 return func(o *options) {
79 sub := o.parent.meta.typeInfo.keyTypeInfo
80 if sub == nil {
81 sub = &typeInfo{}
82 o.parent.meta.typeInfo.keyTypeInfo = sub
83 }
84 o.sharedType = sub
85 }
86 }
87
View as plain text