forked from ivajloip/goyang
/
types_builtin.go
985 lines (896 loc) · 24.5 KB
/
types_builtin.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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
// Copyright 2015 Google Inc.
//
// 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 yang
// This module contains all the builtin types as well as types related
// to types (such as ranges, enums, etc).
import (
"errors"
"fmt"
"math"
"sort"
"strconv"
"strings"
)
// This file handles interpretation of types
// TypeKind is the enumeration of the base types available in YANG. It
// is analogous to reflect.Kind.
type TypeKind uint
const (
// Ynone represents the invalid (unset) type.
Ynone = TypeKind(iota)
// Yint8 is an int in the range [-128, 127].
Yint8
// Yint16 is an int in the range [-32768, 32767].
Yint16
// Yint32 is an int in the range [-2147483648, 2147483647].
Yint32
// Yint64 is an int in the range [-9223372036854775808, 9223372036854775807]
Yint64
// Yuint8 is an int in the range [0, 255]
Yuint8
// Yuint16 is an int in the range [0, 65535]
Yuint16
// Yuint32 is an int in the range [0, 4294967295]
Yuint32
// Yuint64 is an int in the range [0, 18446744073709551615]
Yuint64
// Ybinary stores arbitrary data.
Ybinary
// Ybits is a named set of bits or flags.
Ybits
// Ybool is true or false.
Ybool
// Ydecimal64 is a signed decimal number.
Ydecimal64
// Yempty has no associated value.
Yempty
// Yenum stores enumerated strings.
Yenum
// Yidentityref stores an extensible enumeration.
Yidentityref
// YinstanceIdentifier stores a reference to a data tree node.
YinstanceIdentifier
// Yleafref stores a reference to a leaf instance.
Yleafref
// Ystring is a human readable string.
Ystring
// Yunion is a choice of types.
Yunion
)
// TypeKindFromName maps the string name used in a YANG file to the enumerated
// TypeKind used in this library.
var TypeKindFromName = map[string]TypeKind{
"none": Ynone,
"int8": Yint8,
"int16": Yint16,
"int32": Yint32,
"int64": Yint64,
"uint8": Yuint8,
"uint16": Yuint16,
"uint32": Yuint32,
"uint64": Yuint64,
"binary": Ybinary,
"bits": Ybits,
"boolean": Ybool,
"decimal64": Ydecimal64,
"empty": Yempty,
"enumeration": Yenum,
"identityref": Yidentityref,
"instance-identifier": YinstanceIdentifier,
"leafref": Yleafref,
"string": Ystring,
"union": Yunion,
}
// TypeKindToName maps the enumerated type used in this library to the string
// used in a YANG file.
var TypeKindToName = map[TypeKind]string{
Ynone: "none",
Yint8: "int8",
Yint16: "int16",
Yint32: "int32",
Yint64: "int64",
Yuint8: "uint8",
Yuint16: "uint16",
Yuint32: "uint32",
Yuint64: "uint64",
Ybinary: "binary",
Ybits: "bits",
Ybool: "boolean",
Ydecimal64: "decimal64",
Yempty: "empty",
Yenum: "enumeration",
Yidentityref: "identityref",
YinstanceIdentifier: "instance-identifier",
Yleafref: "leafref",
Ystring: "string",
Yunion: "union",
}
func (k TypeKind) String() string {
if s := TypeKindToName[k]; s != "" {
return s
}
return fmt.Sprintf("unknown-type-%d", k)
}
// A EnumType represents a mapping of strings to integers. It is used both
// for enumerations as well as bitfields.
type EnumType struct {
last int64 // maximum value assigned thus far
min int64 // minimum value allowed
max int64 // maximum value allowed
unique bool // numeric values must be unique (enums)
toString map[int64]string
toInt map[string]int64
}
// NewEnumType returns an initialized EnumType.
func NewEnumType() *EnumType {
return &EnumType{
last: -1, // +1 will start at 0
min: MinEnum,
max: MaxEnum,
unique: true,
toString: map[int64]string{},
toInt: map[string]int64{},
}
}
// NewBitfield returns an EnumType initialized as a bitfield. Multiple string
// values may map to the same numeric values. Numeric values must be small
// non-negative integers.
func NewBitfield() *EnumType {
return &EnumType{
last: -1, // +1 will start at 0
min: 0,
max: MaxBitfieldSize - 1,
toString: map[int64]string{},
toInt: map[string]int64{},
}
}
// Set sets name in e to the provided value. Set returns an error if the value
// is invalid, name is already signed, or when used as an enum rather than a
// bitfield, the value has previousl been used. When two different names are
// assigned to the same value, the conversion from value to name will result in
// the most recently assigned name.
func (e *EnumType) Set(name string, value int64) error {
if _, ok := e.toInt[name]; ok {
return fmt.Errorf("field %s already assigned", name)
}
if oname, ok := e.toString[value]; e.unique && ok {
return fmt.Errorf("fields %s and %s conflict on value %d", name, oname, value)
}
if value < e.min {
return fmt.Errorf("value %d for %s too small (minimum is %d)", value, name, e.min)
}
if value > e.max {
return fmt.Errorf("value %d for %s too large (maximum is %d)", value, name, e.max)
}
e.toString[value] = name
e.toInt[name] = value
if value >= e.last {
e.last = value
}
return nil
}
// SetNext sets the name in e using the next possible value that is greater than
// all previous values.
func (e *EnumType) SetNext(name string) error {
if e.last == MaxEnum {
return fmt.Errorf("enum must specify value")
}
return e.Set(name, e.last+1)
}
// Name returns the name in e associated with value. The empty string is
// returned if no name has been assigned to value.
func (e *EnumType) Name(value int64) string { return e.toString[value] }
// Value returns the value associated with name in e associated. 0 is returned
// if name is not in e, or if it is the first value in an unnumbered enum. Use
// IsDefined to definitively confirm name is in e.
func (e *EnumType) Value(name string) int64 { return e.toInt[name] }
// IsDefined returns true if name is defined in e, else false.
func (e *EnumType) IsDefined(name string) bool {
_, defined := e.toInt[name]
return defined
}
// Names returns the sorted list of enum string names.
func (e *EnumType) Names() []string {
names := make([]string, len(e.toInt))
i := 0
for name := range e.toInt {
names[i] = name
i++
}
sort.Strings(names)
return names
}
type int64Slice []int64
func (p int64Slice) Len() int { return len(p) }
func (p int64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Values returns the sorted list of enum values.
func (e *EnumType) Values() []int64 {
values := make([]int64, len(e.toInt))
i := 0
for _, value := range e.toInt {
values[i] = value
i++
}
sort.Sort(int64Slice(values))
return values
}
// NameMap returns a map of names to values.
func (e *EnumType) NameMap() map[string]int64 {
m := make(map[string]int64, len(e.toInt))
for name, value := range e.toInt {
m[name] = value
}
return m
}
// ValueMap returns a map of values to names.
func (e *EnumType) ValueMap() map[int64]string {
m := make(map[int64]string, len(e.toString))
for name, value := range e.toString {
m[name] = value
}
return m
}
// A YangType is the internal representation of a type in YANG. It may
// refer to either a builtin type or type specified with typedef. Not
// all fields in YangType are used for all types.
type YangType struct {
Name string
Kind TypeKind // Ynone if not a base type
Base *Type `json:"-"` // Base type for non-builtin types
IdentityBase *Identity `json:",omitempty"` // Base statement for a type using identityref
Root *YangType `json:"-"` // root of this type that is the same
Bit *EnumType `json:",omitempty"` // bit position, "status" is lost
Enum *EnumType `json:",omitempty"` // enum name to value, "status" is lost
Units string `json:",omitempty"` // units to be used for this type
Default string `json:",omitempty"` // default value, if any
FractionDigits int `json:",omitempty"` // decimal64 fixed point precision
Length YangRange `json:",omitempty"` // this should be processed by section 12
OptionalInstance bool `json:",omitempty"` // !require-instances which defaults to true
Path string `json:",omitempty"` // the path in a leafref
Pattern []string `json:",omitempty"` // limiting XSD-TYPES expressions on strings
Range YangRange `json:",omitempty"` // range for integers
Type []*YangType `json:",omitempty"` // for unions
}
// BaseTypedefs is a map of all base types to the Typedef structure manufactured
// for the type.
var BaseTypedefs = map[string]*Typedef{}
// typedef returns a Typedef created from y for insertion into the BaseTypedefs
// map.
func (y *YangType) typedef() *Typedef {
return &Typedef{
Name: y.Name,
Source: &Statement{},
Type: &Type{
Name: y.Name,
Source: &Statement{},
YangType: y,
},
YangType: y,
}
}
// ssEqual returns true if the two slices are equivalent.
func ssEqual(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
}
for x, s := range s1 {
if s != s2[x] {
return false
}
}
return true
}
// tsEqual returns true if the two Type slices are identical.
func tsEqual(t1, t2 []*YangType) bool {
if len(t1) != len(t2) {
return false
}
// For now we compare absolute pointers.
// This may be wrong.
for x, t := range t1 {
if !t.Equal(t2[x]) {
return false
}
}
return true
}
// Equal returns true if y and t describe the same type.
func (y *YangType) Equal(t *YangType) bool {
switch {
case
// Don't check the Name, it contains no information
y.Kind != t.Kind,
y.Units != t.Units,
y.Default != t.Default,
y.FractionDigits != t.FractionDigits,
y.IdentityBase != t.IdentityBase,
len(y.Length) != len(t.Length),
!y.Length.Equal(t.Length),
y.OptionalInstance != t.OptionalInstance,
y.Path != t.Path,
!ssEqual(y.Pattern, t.Pattern),
len(y.Range) != len(t.Range),
!y.Range.Equal(t.Range),
!tsEqual(y.Type, t.Type):
return false
}
// TODO(borman): Base, Bit, Enum
return true
}
// Install builtin types as know types
func init() {
for k, v := range baseTypes {
// Base types are always their own root
v.Root = v
BaseTypedefs[k] = v.typedef()
}
}
var baseTypes = map[string]*YangType{
"int8": &YangType{
Name: "int8",
Kind: Yint8,
Range: Int8Range,
},
"int16": &YangType{
Name: "int16",
Kind: Yint16,
Range: Int16Range,
},
"int32": &YangType{
Name: "int32",
Kind: Yint32,
Range: Int32Range,
},
"int64": &YangType{
Name: "int64",
Kind: Yint64,
Range: Int64Range,
},
"uint8": &YangType{
Name: "uint8",
Kind: Yuint8,
Range: Uint8Range,
},
"uint16": &YangType{
Name: "uint16",
Kind: Yuint16,
Range: Uint16Range,
},
"uint32": &YangType{
Name: "uint32",
Kind: Yuint32,
Range: Uint32Range,
},
"uint64": &YangType{
Name: "uint64",
Kind: Yuint64,
Range: Uint64Range,
},
"decimal64": &YangType{
Name: "decimal64",
Kind: Ydecimal64,
Range: Decimal64Range,
},
"string": &YangType{
Name: "string",
Kind: Ystring,
},
"boolean": &YangType{
Name: "boolean",
Kind: Ybool,
},
"enumeration": &YangType{
Name: "enumeration",
Kind: Yenum,
},
"bits": &YangType{
Name: "bits",
Kind: Ybits,
},
"binary": &YangType{
Name: "binary",
Kind: Ybinary,
},
"leafref": &YangType{
Name: "leafref",
Kind: Yleafref,
},
"identityref": &YangType{
Name: "identityref",
Kind: Yidentityref,
},
"empty": &YangType{
Name: "empty",
Kind: Yempty,
},
"union": &YangType{
Name: "union",
Kind: Yunion,
},
"instance-identifier": &YangType{
Name: "instance-identifier",
Kind: YinstanceIdentifier,
},
}
// These are the default ranges defined by the YANG standard.
var (
Int8Range = mustParseRanges("-128..127")
Int16Range = mustParseRanges("-32768..32767")
Int32Range = mustParseRanges("-2147483648..2147483647")
Int64Range = mustParseRanges("-9223372036854775808..9223372036854775807")
Uint8Range = mustParseRanges("0..255")
Uint16Range = mustParseRanges("0..65535")
Uint32Range = mustParseRanges("0..4294967295")
Uint64Range = mustParseRanges("0..18446744073709551615")
Decimal64Range = mustParseRanges("min..max")
)
const (
// MaxInt64 corresponds to the maximum value of a signed int64.
MaxInt64 = 1<<63 - 1
// MinInt64 corresponds to the maximum value of a signed int64.
MinInt64 = -1 << 63
// AbsMinInt64 is the absolute value of MinInt64.
AbsMinInt64 = 1 << 63
// MaxEnum is the maximum value of an enumeration.
MaxEnum = 1<<31 - 1
// MinEnum is the minimum value of an enumeration.
MinEnum = -1 << 31
// MaxBitfieldSize is the maximum number of bits in a bitfield.
MaxBitfieldSize = 1 << 32
// MaxFractionDigits is the maximum number of fractional digits as per RFC6020 Section 9.3.4.
MaxFractionDigits uint8 = 18
)
type NumberKind int
const (
// Positive indicates that a Number is non-negative.
Positive = NumberKind(iota)
// Negative indicates that a Number is negative.
Negative
// MinNumber indicates that the Number is the minimum value allowed for the range.
MinNumber
// MaxNumber indicates that the Number is the maximum value allowed for the range.
MaxNumber
)
const space18 = "000000000000000000" // used for prepending 0's
// A Number is either an integer the range of [-(1<<64) - 1, (1<<64)-1], or a
// YANG decimal conforming to https://tools.ietf.org/html/rfc6020#section-9.3.4.
type Number struct {
// Kind is the kind of number (+/-ve, min/max).
Kind NumberKind
// Absolute value of the number.
Value uint64
// Number of fractional digits.
FractionDigits uint8
}
var maxNumber = Number{Kind: MaxNumber}
var minNumber = Number{Kind: MinNumber}
// IsDecimal reports whether n is a decimal number.
func (n Number) IsDecimal() bool {
return n.FractionDigits != 0
}
// FromInt creates a Number from an int64.
func FromInt(i int64) Number {
if i < 0 {
return Number{Kind: Negative, Value: uint64(-i)}
}
return Number{Kind: Positive, Value: uint64(i)}
}
// FromUint creates a Number from a uint64.
func FromUint(i uint64) Number {
return Number{Kind: Positive, Value: i}
}
// FromFloat creates a Number from a float64. Input values with absolute value
// larger than MaxInt64/MinInt64 are converted into maxNumber/minNumber.
func FromFloat(f float64) Number {
if f > float64(MaxInt64) {
return maxNumber
}
if f < float64(MinInt64) {
return minNumber
}
var fracDig uint8
for ; Frac(f) != 0.0 && fracDig <= MaxFractionDigits; fracDig++ {
f *= 10.0
}
v := uint64(f)
kind := Positive
if f < 0 {
kind = Negative
v = -v
}
n := Number{Kind: kind, Value: v, FractionDigits: fracDig}
return n
}
// ParseNumber returns s as a Number. Numbers may be represented in decimal,
// octal, or hexadecimal using the standard prefix notations (e.g., 0 and 0x)
func ParseNumber(s string) (n Number, err error) {
s = strings.TrimSpace(s)
switch s {
case "max":
return maxNumber, nil
case "min":
return minNumber, nil
case "":
return n, errors.New("converting empty string to number")
case "+", "-":
return n, errors.New("sign with no value")
}
n.Kind = Positive
ns := s
switch s[0] {
case '+':
ns = s[1:]
case '-':
n.Kind = Negative
ns = s[1:]
}
n.Value, err = strconv.ParseUint(ns, 0, 64)
if err == nil {
return n, nil
}
return DecimalValueFromString(s, -1)
}
// DecimalValueFromString returns a decimal Number representation of inStr.
// If fracDigRequired is >= 0, the number is represented with fracDigRequired
// fractional digits, regardless of the precision of numStr, otherwise the
// precision of numStr is used to set the number of fractional digits.
// numStr must conform to Section 9.3.4.
func DecimalValueFromString(numStr string, fracDigRequired int) (n Number, err error) {
if fracDigRequired > int(MaxFractionDigits) {
return n, fmt.Errorf("too many fraction digits %d > max of %d", fracDigRequired, MaxFractionDigits)
}
s := numStr
dx := strings.Index(s, ".")
fracDig := 0
if dx >= 0 {
fracDig = len(s) - 1 - dx
// remove first decimal, if dx > 1, will fail ParseInt below
s = s[:dx] + s[dx+1:]
}
if fracDigRequired < 0 {
fracDigRequired = fracDig
}
if fracDig > fracDigRequired {
return n, fmt.Errorf("%s has too much precision, expect <= %d fractional digits", s, fracDigRequired)
}
s += space18[:fracDigRequired-fracDig]
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return n, fmt.Errorf("%s is not a valid decimal number: %s", numStr, err)
}
kind := Positive
if v < 0 {
kind = Negative
v = -v
}
return Number{Kind: kind, Value: uint64(v), FractionDigits: uint8(fracDig)}, nil
}
// String returns n as a string in decimal.
func (n Number) String() string {
var out string
switch n.Kind {
case MinNumber:
return "min"
case MaxNumber:
return "max"
}
out += strconv.FormatUint(n.Value, 10)
if n.IsDecimal() {
fd := int(n.FractionDigits)
if fd > 0 {
ofd := len(out) - fd
if ofd <= 0 {
// We want 0.1 not .1
out = space18[:-ofd+1] + out
ofd = 1
}
out = out[:ofd] + "." + out[ofd:]
}
}
if n.Kind == Negative {
out = "-" + out
}
return out
}
// Int returns n as an int64. It returns an error if n overflows an int64 or
// the number is decimal.
func (n Number) Int() (int64, error) {
nv := n.Value
if n.IsDecimal() {
nv = n.Value / uint64(math.Pow10(int(n.FractionDigits)))
}
switch n.Kind {
case MinNumber:
return MinInt64, nil
case MaxNumber:
return MaxInt64, nil
case Negative:
switch {
case nv == AbsMinInt64:
return MinInt64, nil
case nv < AbsMinInt64:
return -int64(nv), nil
}
case Positive:
if n.Value <= MaxInt64 {
return int64(nv), nil
}
return 0, errors.New("signed integer overflow")
default:
}
return 0, errors.New("unknown number type")
}
// add adds i to n without checking overflow. We really only need to be
// able to add 1 for our code. panics if n is a decimal.
func (n Number) add(i uint64) Number {
if n.IsDecimal() {
panic("cannot call add() on decimal number " + n.String())
}
switch n.Kind {
case MinNumber:
return n
case MaxNumber:
return n
case Negative:
if n.Value <= i {
n.Value = i - n.Value
n.Kind = Positive
} else {
n.Value -= i
}
case Positive:
n.Value += i
default:
panic("add to unknown number type")
}
return n
}
// Less returns true if n is less than m. Panics if n and m are a mix of integer
// and decimal.
func (n Number) Less(m Number) bool {
switch {
case m.Kind == MinNumber:
return false
case n.Kind == MinNumber:
return true
case n.Kind == MaxNumber:
return false
case m.Kind == MaxNumber:
return true
case n.Kind == Negative && m.Kind != Negative:
return true
case n.Kind != Negative && m.Kind == Negative:
return false
}
nt, mt := n.Trunc(), m.Trunc()
lt := nt < mt
if nt == mt {
nf, mf := n.frac(), m.frac()
if nf == mf {
return false
}
lt = nf < mf
}
if n.Kind == Negative {
return !lt
}
return lt
}
// Equal returns true if n is equal to m.
func (n Number) Equal(m Number) bool {
return !n.Less(m) && !m.Less(n)
}
// Trunc returns the whole part of abs(n) as a signed integer.
func (n Number) Trunc() uint64 {
nv := n.Value
e := pow10(n.FractionDigits)
return nv / e
}
// frac returns the fraction part with a precision of 18 fractional digits.
// E.g. if n is 3.1 then n.frac() returns 100,000,000,000,000,000
func (n Number) frac() uint64 {
frac := n.FractionDigits
i := n.Trunc() * pow10(frac)
return (n.Value - i) * pow10(uint8(18-frac))
}
// YRange is a single range of consecutive numbers, inclusive.
type YRange struct {
Min Number
Max Number
}
// Valid returns false if r is not a valid range (min > max).
func (r YRange) Valid() bool {
return !r.Max.Less(r.Min)
}
// A YangRange is a set of non-overlapping ranges.
type YangRange []YRange
// ParseRanges parses s into a series of ranges. Each individual range is
// in s is separated by the pipe character (|). The min and max value of
// a range are separated by "..". An error is returned if the range is
// invalid. The resulting range is sorted and coalesced.
func ParseRanges(s string) (YangRange, error) {
parts := strings.Split(s, "|")
r := make(YangRange, len(parts))
for i, s := range parts {
parts := strings.Split(s, "..")
min, err := ParseNumber(parts[0])
if err != nil {
return nil, err
}
var max Number
switch len(parts) {
case 1:
max = min
case 2:
max, err = ParseNumber(parts[1])
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("two many ..'s in %s", s)
}
if max.Less(min) {
return nil, fmt.Errorf("%s less than %s", max, min)
}
r[i] = YRange{min, max}
}
if err := r.Validate(); err != nil {
return nil, err
}
return coalesce(r), nil
}
// coalesce coalesces r into as few ranges as possible. For example,
// 1..5|6..10 would become 1..10. r is assumed to be sorted.
// r is assumed to be valid (see Validate)
func coalesce(r YangRange) YangRange {
// coalesce the ranges if we have more than 1.
if len(r) < 2 {
return r
}
cr := make(YangRange, len(r))
i := 0
cr[i] = r[0]
for _, r1 := range r[1:] {
// r1.Min is always at least as large as cr[i].Min
// Cases are:
// r1 is contained in cr[i]
// r1 starts inside of cr[i]
// r1.Min cr[i].Max+1
// r1 is beyond cr[i]
if cr[i].Max.add(1).Less(r1.Min) {
// r1 starts after cr[i], this is a new range
i++
cr[i] = r1
} else if cr[i].Max.Less(r1.Max) {
cr[i].Max = r1.Max
}
}
return cr[:i+1]
}
func mustParseRanges(s string) YangRange {
r, err := ParseRanges(s)
if err != nil {
panic(err)
}
return r
}
// String returns r as a string using YANG notation, either a simple
// value if min == max or min..max.
func (r YRange) String() string {
if r.Min.Equal(r.Max) {
return r.Min.String()
}
return r.Min.String() + ".." + r.Max.String()
}
// String returns the ranges r using YANG notation. Individual ranges
// are separated by pipes (|).
func (r YangRange) String() string {
s := make([]string, len(r))
for i, r := range r {
s[i] = r.String()
}
return strings.Join(s, "|")
}
func (r YangRange) Len() int { return len(r) }
func (r YangRange) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r YangRange) Less(i, j int) bool {
switch {
case r[i].Min.Less(r[j].Min):
return true
case r[j].Min.Less(r[i].Min):
return false
default:
return r[i].Max.Less(r[j].Max)
}
}
// Validate sorts r and returns an error if r has either an invalid range or has
// overlapping ranges.
func (r YangRange) Validate() error {
sort.Sort(r)
switch {
case len(r) == 0:
return nil
case !r[0].Valid():
return errors.New("invalid number")
}
p := r[0]
for _, n := range r[1:] {
if n.Min.Less(p.Max) {
return errors.New("overlapping ranges")
}
}
return nil
}
// Equal returns true if ranges r and q are identically equivalent.
// TODO(borman): should we coalesce ranges in the comparison?
func (r YangRange) Equal(q YangRange) bool {
if len(r) != len(q) {
return false
}
for i, r := range r {
if r != q[i] {
return false
}
}
return true
}
// Contains returns true if all possible values in s are also possible values
// in r. An empty range is assumed to be min..max.
func (r YangRange) Contains(s YangRange) bool {
if len(s) == 0 || len(r) == 0 {
return true
}
rc := make(chan YRange)
go func() {
for _, v := range r {
rc <- v
}
close(rc)
}()
// All ranges are sorted and coalesced which means each range
// in s must exist
// We know rc will always produce at least one value
rr, ok := <-rc
for _, ss := range s {
// min is always within range
if ss.Min.Kind != MinNumber {
for rr.Max.Less(ss.Min) {
rr, ok = <-rc
if !ok {
return false
}
}
}
if (ss.Max.Kind == MaxNumber) || (ss.Min.Kind == MinNumber) {
continue
}
if ss.Min.Less(rr.Min) || rr.Max.Less(ss.Max) {
return false
}
}
return true
}
// Frac returns the fractional part of f.
func Frac(f float64) float64 {
return f - math.Trunc(f)
}
// pow10 returns 10^e without checking for overflow.
func pow10(e uint8) uint64 {
var out uint64 = 1
for i := uint8(0); i < e; i++ {
out *= 10
}
return out
}