-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.go
917 lines (802 loc) · 23.1 KB
/
group.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
// Copyright 2019 xgfone
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gconf
import (
"fmt"
"sort"
"strings"
"sync"
"time"
)
var errHasBeenRegistered = fmt.Errorf("has been registered")
var (
// ErrNoOpt is returned when no corresponding option.
ErrNoOpt = fmt.Errorf("no option")
// ErrFrozenOpt is returned when to set the value of the option but it's forzen.
ErrFrozenOpt = fmt.Errorf("option is frozen")
)
// IsErrNoOpt whether reports the error is ErrNoOpt or not.
func IsErrNoOpt(err error) bool {
if e, ok := err.(OptError); (ok && e.Err == ErrNoOpt) || err == ErrNoOpt {
return true
}
return false
}
// IsErrFrozenOpt whether reports the error is ErrFrozenOpt or not.
func IsErrFrozenOpt(err error) bool {
if e, ok := err.(OptError); ok && e.Err == ErrFrozenOpt {
return true
}
return false
}
// OptError is used to represent an error about option.
type OptError struct {
Group string
Opt string
Err error
Value interface{}
}
// NewOptError returns a new OptError.
func NewOptError(group, opt string, err error, value interface{}) OptError {
return OptError{Group: group, Opt: opt, Err: err, Value: value}
}
func (e OptError) Error() string {
if e.Group == "" {
return fmt.Sprintf("[Config] invalid setting for '%s': %s", e.Opt, e.Err)
}
return fmt.Sprintf("[Config] invalid setting for '%s:%s': %s", e.Group, e.Opt, e.Err)
}
type groupOpt struct {
opt Opt
value interface{}
watch func(interface{})
frozen bool
}
// OptGroup is the group of the options.
type OptGroup struct {
lock sync.RWMutex
conf *Config
name string // long full name, such as "group.subgroup1.subgroup2".
opts map[string]*groupOpt
alias map[string]string
migrate map[string]string
frozen bool
}
func newOptGroup(conf *Config, name string) *OptGroup {
return &OptGroup{
conf: conf,
name: name,
opts: make(map[string]*groupOpt, 16),
alias: make(map[string]string, 4),
migrate: make(map[string]string, 4),
}
}
// NewGroup returns a new sub-group with the name named `group`.
//
// Notice: If the sub-group has existed, it will return it instead.
func (g *OptGroup) NewGroup(group string) *OptGroup {
return g.conf.newGroup(g.name, group)
}
// Config returns the Config that the current group belongs to.
func (g *OptGroup) Config() *Config {
return g.conf
}
// Name returns the full name of the current group.
func (g *OptGroup) Name() string {
return g.name
}
// Group returns the sub-group named group.
//
// It supports the cascaded group name, for example, the following ways are equal.
//
// g.Group("group1.group2.group3")
// g.Group("group1").Group("group2.group3")
// g.Group("group1.group2").Group("group3")
// g.Group("group1").Group("group2").Group("group3")
//
// Notice: if the group is "", it will return the current group.
func (g *OptGroup) Group(group string) *OptGroup {
return g.conf.getGroup(g.name, group)
}
// G is the alias of Group.
func (g *OptGroup) G(group string) *OptGroup {
return g.Group(group)
}
// MustGroup is equal to g.Group(group), but panic if the group does not exist.
func (g *OptGroup) MustGroup(group string) *OptGroup {
if _g := g.Group(group); _g != nil {
return _g
}
panic(fmt.Errorf("no group '%s'", group))
}
// MustG is short for g.MustGroup(group).
func (g *OptGroup) MustG(group string) *OptGroup {
return g.MustGroup(group)
}
// AllOpts returns all the options in the current group.
func (g *OptGroup) AllOpts() []Opt {
g.lock.RLock()
opts := make([]Opt, len(g.opts))
var index int
for _, opt := range g.opts {
opts[index] = opt.opt
index++
}
g.lock.RUnlock()
sort.Slice(opts, func(i, j int) bool { return opts[i].Name < opts[j].Name })
return opts
}
func (g *OptGroup) fixOptName(name string) string {
if name == "" {
panic("the option name must not be empty")
}
return strings.Replace(name, "-", "_", -1)
}
// Opt returns the option named name.
func (g *OptGroup) Opt(name string) (opt Opt, exist bool) {
name = g.fixOptName(name)
g.lock.RLock()
gopt := g.opts[name]
if gopt == nil {
gopt = g.opts[g.alias[name]] // Check the alias
}
g.lock.RUnlock()
if gopt != nil {
return gopt.opt, true
}
return
}
// MustOpt is the same as Opt(name), but panic if the option does not exist.
func (g *OptGroup) MustOpt(name string) Opt {
if opt, ok := g.Opt(name); ok {
return opt
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// HasOpt reports whether there is an option named name in the current group.
func (g *OptGroup) HasOpt(name string) bool {
name = g.fixOptName(name)
g.lock.RLock()
_, ok := g.opts[name]
if !ok {
if name = g.alias[name]; name != "" {
_, ok = g.opts[name]
}
}
g.lock.RUnlock()
return ok
}
// OptIsSet reports whether the option named name has been set.
func (g *OptGroup) OptIsSet(name string) bool {
name = g.fixOptName(name)
var set bool
g.lock.RLock()
if opt, ok := g.opts[name]; ok {
set = opt.value != nil
} else if name, ok = g.alias[name]; ok {
if opt, ok = g.opts[name]; ok {
set = opt.value != nil
}
}
g.lock.RUnlock()
return set
}
// HasOptAndIsNotSet reports whether the option named name exists and
// hasn't been set, which is equal to `g.HasOpt(name) && !g.OptIsSet(name)`.
func (g *OptGroup) HasOptAndIsNotSet(name string) (yes bool) {
name = g.fixOptName(name)
g.lock.RLock()
if opt, ok := g.opts[name]; ok {
yes = opt.value == nil
} else if name, ok = g.alias[name]; ok {
if opt, ok = g.opts[name]; ok {
yes = opt.value == nil
}
}
g.lock.RUnlock()
return
}
func (g *OptGroup) setOptAlias(old, new string) {
old = g.fixOptName(old)
new = g.fixOptName(new)
if old == "" || new == "" {
return
}
for name, opt := range g.opts {
if name == new {
var exist bool
for _, alias := range opt.opt.Aliases {
if alias == old {
exist = true
break
}
}
if !exist {
opt.opt.Aliases = append(opt.opt.Aliases, old)
}
}
}
g.alias[old] = new
debugf("[Config] Set the option alias from '%s' to '%s' in the group '%s'",
old, new, g.name)
}
func (g *OptGroup) unsetOptAlias(name string) {
delete(g.alias, name)
for oldname, newname := range g.alias {
if newname == name {
delete(g.alias, oldname)
debugf("[Config] Unset the option alias from '%s' to '%s' in the group '%s'",
oldname, newname, g.name)
}
}
}
// SetOptAlias sets the alias of the option from old to new. So you can access
// the option named new by the old name.
//
// Notice: it does nothing if either is "".
func (g *OptGroup) SetOptAlias(old, new string) {
g.lock.Lock()
g.setOptAlias(old, new)
g.lock.Unlock()
}
func (g *OptGroup) unmigrate(name string) {
delete(g.migrate, name)
for oldname, newname := range g.migrate {
if newname == name {
delete(g.migrate, oldname)
}
}
}
// Migrate migrates the old option oldOptName to the other option of the other
// group, newGroupOptName, which will update the value of newGroupOptName to
// the new when the value of oldOptName is updated.
func (g *OptGroup) Migrate(oldOptName, newGroupOptName string) {
oldOptName = g.fixOptName(oldOptName)
if oldOptName == "" || newGroupOptName == "" {
panic("the key must not be empty")
}
// Check the cycle
key := oldOptName
if g.name != "" {
key = fmt.Sprintf("%s%s%s", g.name, g.conf.gsep, oldOptName)
}
cycles := []string{key, newGroupOptName}
gname, opt := g.getGroupAndOpt(newGroupOptName)
for {
group := g.conf.Group(gname)
if group == nil {
break
}
if newMigrate, ok := group.migrate[opt]; ok {
for _, s := range cycles {
if s == newMigrate {
cycles = append(cycles, newMigrate)
panic(fmt.Errorf("migrating cycle: %s", strings.Join(cycles, "->")))
}
}
cycles = append(cycles, newMigrate)
gname, opt = g.getGroupAndOpt(newMigrate)
} else {
break
}
}
g.migrate[oldOptName] = newGroupOptName
}
func (g *OptGroup) getGroupAndOpt(name string) (string, string) {
if index := strings.Index(name, g.conf.gsep); index > -1 {
return name[:index], name[index+len(g.conf.gsep):]
}
return "", name
}
/// --------------------------------------------------------------------------
func (g *OptGroup) setOptWatch(name string, watch func(interface{})) {
name = g.fixOptName(name)
g.lock.Lock()
if opt, ok := g.opts[name]; ok {
opt.watch = watch
} else if name, ok = g.alias[name]; ok {
if opt, ok = g.opts[name]; ok {
opt.watch = watch
}
}
g.lock.Unlock()
}
func (g *OptGroup) registerOpts(opts ...Opt) {
names := make([]string, len(opts))
for i := range opts {
opts[i].check()
if err := opts[i].fix(); err != nil {
panic(NewOptError(g.name, opts[i].Name, err, opts[i].Default))
} else if err := opts[i].validate(opts[i].Default); err != nil {
panic(NewOptError(g.name, opts[i].Name, err, opts[i].Default))
}
names[i] = g.fixOptName(opts[i].Name)
}
g.lock.Lock()
for i, name := range names {
if _, ok := g.opts[name]; ok {
g.lock.Unlock()
panic(NewOptError(g.name, opts[i].Name, errHasBeenRegistered, opts[i].Default))
}
}
for i, opt := range opts {
debugf("[Config] Register the option '%s' into the group '%s'", opt.Name, g.name)
g.opts[names[i]] = &groupOpt{opt: opt}
for _, alias := range opt.Aliases {
g.setOptAlias(alias, opt.Name)
}
}
g.lock.Unlock()
}
func (g *OptGroup) unRegisterOpt(opt string) {
name := g.fixOptName(opt)
delete(g.opts, name)
g.unmigrate(name)
g.unsetOptAlias(name)
}
// RegisterOpts registers a set of options and returns true.
//
// Notice: if a certain option has existed, it will panic.
func (g *OptGroup) RegisterOpts(opts ...Opt) {
g.registerOpts(opts...)
}
// UnregisterOpts unregisters the registered options.
func (g *OptGroup) UnregisterOpts(opts ...Opt) {
g.lock.Lock()
defer g.lock.Unlock()
for _, opt := range opts {
g.unRegisterOpt(opt.Name)
}
}
/// --------------------------------------------------------------------------
// FreezeGroup freezes the current group and disable its options to be set.
//
// If the current group has been frozen, it does nothing.
func (g *OptGroup) FreezeGroup() {
g.lock.Lock()
if !g.frozen {
g.frozen = true
}
g.lock.Unlock()
}
// UnfreezeGroup unfreezes the current group and allows its options to be set.
//
// If the current group has been unfrozen, it does nothing.
func (g *OptGroup) UnfreezeGroup() {
g.lock.Lock()
if g.frozen {
g.frozen = false
}
g.lock.Unlock()
}
// FreezeOpt freezes these options and disable them to be set.
//
// If the option does not exist has been frozen, it does nothing for it.
func (g *OptGroup) FreezeOpt(names ...string) {
for i := range names {
names[i] = g.fixOptName(names[i])
}
g.lock.Lock()
for _, name := range names {
gopt, ok := g.opts[name]
if !ok {
if alias, exist := g.alias[name]; exist {
gopt, ok = g.opts[alias]
}
}
if ok && !gopt.frozen {
gopt.frozen = true
}
}
g.lock.Unlock()
}
// UnfreezeOpt unfreeze these options and allows them to be set.
//
// If the option does not exist has been unfrozen, it does nothing for it.
func (g *OptGroup) UnfreezeOpt(names ...string) {
for i := range names {
names[i] = g.fixOptName(names[i])
}
g.lock.Lock()
for _, name := range names {
gopt, ok := g.opts[name]
if !ok {
if alias, exist := g.alias[name]; exist {
gopt, ok = g.opts[alias]
}
}
if ok && gopt.frozen {
gopt.frozen = false
}
}
g.lock.Unlock()
}
// GroupIsFrozen reports whether the current group named name is frozen.
func (g *OptGroup) GroupIsFrozen() (frozen bool) {
g.lock.RLock()
frozen = g.frozen
g.lock.RUnlock()
return
}
// OptIsFrozen reports whether the option named name is frozen.
//
// Return false if the option does not exist.
func (g *OptGroup) OptIsFrozen(name string) (frozen bool) {
name = g.fixOptName(name)
g.lock.RLock()
frozen = g.optIsFrozen(name)
g.lock.RUnlock()
return
}
func (g *OptGroup) optIsFrozen(name string) bool {
if g.frozen {
return true
} else if gopt, ok := g.opts[name]; ok {
return gopt.frozen
} else if name, ok = g.alias[name]; ok {
if gopt, ok = g.opts[name]; ok {
return gopt.frozen
}
}
return false
}
/// --------------------------------------------------------------------------
func (g *OptGroup) parseOptValue(name string, value interface{}) (interface{}, error) {
if value == nil {
return nil, nil
}
opt, ok := g.opts[name]
if !ok {
if name, ok = g.alias[name]; !ok {
return nil, ErrNoOpt
} else if opt, ok = g.opts[name]; !ok {
return nil, ErrNoOpt
}
}
// Parse the option value
v, err := opt.opt.Parser(value)
if err != nil {
return nil, NewOptError(g.name, opt.opt.Name, err, value)
}
// Fix the parsed value
if opt.opt.Fix != nil {
_v, err := opt.opt.Fix(v)
if err != nil {
return nil, NewOptError(g.name, opt.opt.Name, err, v)
}
v = _v
}
// Validate the option value
if err = opt.opt.validate(v); err != nil {
return nil, NewOptError(g.name, opt.opt.Name, err, v)
}
return v, nil
}
// Parse the value of the option named name, which will call the parser and
// the validators of this option.
//
// If no the option named `name`, it will return ErrNoOpt.
// If there is an other error, it will return an OptError.
func (g *OptGroup) Parse(name string, value interface{}) (interface{}, error) {
name = g.fixOptName(name)
g.lock.RLock()
value, err := g.parseOptValue(name, value)
g.lock.RUnlock()
return value, err
}
func (g *OptGroup) setOptValue(name string, value interface{}) (
string, string, interface{}, interface{}, []func(interface{})) {
opt := g.opts[name]
if opt == nil {
name = g.alias[name]
opt = g.opts[name]
}
old := opt.value
opt.value = value
if old == nil {
old = opt.opt.Default
}
if opt.watch != nil {
opt.watch(value)
}
if g.name == "" {
debugf("[Config] Set [%s] to '%v'", name, value)
} else {
debugf("[Config] Set [%s:%s] to '%v'", g.name, name, value)
}
return g.migrate[name], name, old, value, opt.opt.Observers
}
func (g *OptGroup) setOptWithLock(name string, value interface{}) {
g.lock.Lock()
if g.optIsFrozen(name) {
g.lock.Unlock()
g.conf.handleError(NewOptError(g.Name(), name, ErrFrozenOpt, value))
return
}
migrate, name, old, new, observers := g.setOptValue(name, value)
g.lock.Unlock()
if migrate != "" {
g.conf.UpdateValue(migrate, new)
}
g.conf.noticeOptChange(g.name, name, old, new, observers)
}
// Set parses and sets the option value named name in the current group to value.
//
// For the option name, the characters "-" and "_" are equal, that's, "abcd-efg"
// is equal to "abcd_efg".
//
// If there is not the option or the value is nil, it will ignore it.
func (g *OptGroup) Set(name string, value interface{}) error {
if value == nil {
return nil
}
name = g.fixOptName(name)
g.lock.Lock()
if g.optIsFrozen(name) {
g.lock.Unlock()
return NewOptError(g.Name(), name, ErrFrozenOpt, value)
}
switch value, err := g.parseOptValue(name, value); err {
case nil:
migrate, name, old, new, observers := g.setOptValue(name, value)
g.lock.Unlock()
if migrate != "" {
g.conf.UpdateValue(migrate, new)
}
g.conf.noticeOptChange(g.name, name, old, new, observers)
return nil
case ErrNoOpt:
g.lock.Unlock()
return NewOptError(g.Name(), name, err, value)
default:
g.lock.Unlock()
return err
}
}
// Get returns the value of the option named name.
//
// Return nil if this option does not exist.
func (g *OptGroup) Get(name string) (value interface{}) {
name = g.fixOptName(name)
g.lock.RLock()
opt, ok := g.opts[name]
if !ok {
if name, ok = g.alias[name]; ok {
opt, ok = g.opts[name]
}
}
if ok {
if value = opt.value; value == nil {
value = opt.opt.Default
}
}
g.lock.RUnlock()
return
}
/// --------------------------------------------------------------------------
// Must is the same as Get(name), but panic if the option does not exist.
func (g *OptGroup) Must(name string) (value interface{}) {
if value = g.Get(name); value == nil {
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
return
}
// GetBool is the same as Get(name), but returns the bool value.
func (g *OptGroup) GetBool(name string) bool {
v, _ := ToBool(g.Get(name))
return v
}
// MustBool is the same as GetBool(name), but panic if the option does not exist.
func (g *OptGroup) MustBool(name string) bool {
if value := g.Get(name); value != nil {
v, _ := ToBool(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetInt is the same as Get(name), but returns the int value.
func (g *OptGroup) GetInt(name string) int {
v, _ := ToInt(g.Get(name))
return v
}
// MustInt is the same as GetInt(name), but panic if the option does not exist.
func (g *OptGroup) MustInt(name string) int {
if value := g.Get(name); value != nil {
v, _ := ToInt(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetInt32 is the same as Get(name), but returns the int32 value.
func (g *OptGroup) GetInt32(name string) int32 {
v, _ := ToInt32(g.Get(name))
return v
}
// MustInt32 is the same as GetInt32(name), but panic if the option does not exist.
func (g *OptGroup) MustInt32(name string) int32 {
if value := g.Get(name); value != nil {
v, _ := ToInt32(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetInt64 is the same as Get(name), but returns the int64 value.
func (g *OptGroup) GetInt64(name string) int64 {
v, _ := ToInt64(g.Get(name))
return v
}
// MustInt64 is the same as GetInt64(name), but panic if the option does not exist.
func (g *OptGroup) MustInt64(name string) int64 {
if value := g.Get(name); value != nil {
v, _ := ToInt64(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetUint is the same as Get(name), but returns the uint value.
func (g *OptGroup) GetUint(name string) uint {
v, _ := ToUint(g.Get(name))
return v
}
// MustUint is the same as GetUint(name), but panic if the option does not exist.
func (g *OptGroup) MustUint(name string) uint {
if value := g.Get(name); value != nil {
v, _ := ToUint(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetUint32 is the same as Get(name), but returns the uint32 value.
func (g *OptGroup) GetUint32(name string) uint32 {
v, _ := ToUint32(g.Get(name))
return v
}
// MustUint32 is the same as GetUint32(name), but panic if the option does not exist.
func (g *OptGroup) MustUint32(name string) uint32 {
if value := g.Get(name); value != nil {
v, _ := ToUint32(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetUint64 is the same as Get(name), but returns the uint64 value.
func (g *OptGroup) GetUint64(name string) uint64 {
v, _ := ToUint64(g.Get(name))
return v
}
// MustUint64 is the same as GetUint64(name), but panic if the option does not exist.
func (g *OptGroup) MustUint64(name string) uint64 {
if value := g.Get(name); value != nil {
v, _ := ToUint64(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetFloat64 is the same as Get(name), but returns the float64 value.
func (g *OptGroup) GetFloat64(name string) float64 {
v, _ := ToFloat64(g.Get(name))
return v
}
// MustFloat64 is the same as GetFloat64(name), but panic if the option does not exist.
func (g *OptGroup) MustFloat64(name string) float64 {
if value := g.Get(name); value != nil {
v, _ := ToFloat64(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetString is the same as Get(name), but returns the string value.
func (g *OptGroup) GetString(name string) string {
v, _ := ToString(g.Get(name))
return v
}
// MustString is the same as GetString(name), but panic if the option does not exist.
func (g *OptGroup) MustString(name string) string {
if value := g.Get(name); value != nil {
v, _ := ToString(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetDuration is the same as Get(name), but returns the time.Duration value.
func (g *OptGroup) GetDuration(name string) time.Duration {
v, _ := ToDuration(g.Get(name))
return v
}
// MustDuration is the same as GetDuration(name), but panic if the option does not exist.
func (g *OptGroup) MustDuration(name string) time.Duration {
if value := g.Get(name); value != nil {
v, _ := ToDuration(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetTime is the same as Get(name), but returns the time.Time value.
func (g *OptGroup) GetTime(name string) time.Time {
v, _ := ToTime(g.Get(name))
return v
}
// MustTime is the same as GetTime(name), but panic if the option does not exist.
func (g *OptGroup) MustTime(name string) time.Time {
if value := g.Get(name); value != nil {
v, _ := ToTime(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetIntSlice is the same as Get(name), but returns the []int value.
func (g *OptGroup) GetIntSlice(name string) []int {
v, _ := ToIntSlice(g.Get(name))
return v
}
// MustIntSlice is the same as GetIntSlice(name), but panic if the option does not exist.
func (g *OptGroup) MustIntSlice(name string) []int {
if value := g.Get(name); value != nil {
v, _ := ToIntSlice(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetUintSlice is the same as Get(name), but returns the []uint value.
func (g *OptGroup) GetUintSlice(name string) []uint {
v, _ := ToUintSlice(g.Get(name))
return v
}
// MustUintSlice is the same as GetUintSlice(name), but panic if the option does not exist.
func (g *OptGroup) MustUintSlice(name string) []uint {
if value := g.Get(name); value != nil {
v, _ := ToUintSlice(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetFloat64Slice is the same as Get(name), but returns the []float64 value.
func (g *OptGroup) GetFloat64Slice(name string) []float64 {
v, _ := ToFloat64Slice(g.Get(name))
return v
}
// MustFloat64Slice is the same as GetFloat64Slice(name), but panic if the option does not exist.
func (g *OptGroup) MustFloat64Slice(name string) []float64 {
if value := g.Get(name); value != nil {
v, _ := ToFloat64Slice(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetStringSlice is the same as Get(name), but returns the []string value.
func (g *OptGroup) GetStringSlice(name string) []string {
v, _ := ToStringSlice(g.Get(name))
return v
}
// MustStringSlice is the same as GetStringSlice(name), but panic if the option does not exist.
func (g *OptGroup) MustStringSlice(name string) []string {
if value := g.Get(name); value != nil {
v, _ := ToStringSlice(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}
// GetDurationSlice is the same as Get(name), but returns the []time.Duration value.
func (g *OptGroup) GetDurationSlice(name string) []time.Duration {
v, _ := ToDurationSlice(g.Get(name))
return v
}
// MustDurationSlice is the same as GetDurationSlice(name), but panic if the option does not exist.
func (g *OptGroup) MustDurationSlice(name string) []time.Duration {
if value := g.Get(name); value != nil {
v, _ := ToDurationSlice(value)
return v
}
panic(NewOptError(g.name, name, ErrNoOpt, nil))
}