-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
1548 lines (1327 loc) · 33.2 KB
/
types.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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2017-2021, AT&T Intellectual Property. All rights reserved
//
// Copyright (c) 2014-2017 by Brocade Communications Systems, Inc.
// All rights reserved.
//
// SPDX-License-Identifier: MPL-2.0
package schema
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/danos/mgmterror"
"github.com/danos/utils/pathutil"
"github.com/danos/yang/xpath"
"github.com/danos/yang/xpath/xutils"
)
/*
* The typesystem for yang is horrbily complex in the way restrictions are applied to types
* to satisfy this we create a golang type that reflects the yang type with a validation
* method. We also define restrictions that are preparsed golang representations of the
* restriction for faster runtime validation. To make matters worse the yang definition doesn't
* lexically specify which type of restriction is allowed on which type instead these are semantic
* restrictions that must be resolved at type creation time.
*/
var errInvalRestriction = errors.New("invalid restriction for type")
var fdtab = map[Fracdigit]Drb{
1: {-922337203685477580.8, 922337203685477580.7},
2: {-92233720368547758.08, 92233720368547758.07},
3: {-9223372036854775.808, 9223372036854775.807},
4: {-922337203685477.5808, 922337203685477.5807},
5: {-92233720368547.75808, 92233720368547.75807},
6: {-9223372036854.775808, 9223372036854.775807},
7: {-922337203685.4775808, 922337203685.4775807},
8: {-92233720368.54775808, 92233720368.54775807},
9: {-9223372036.854775808, 9223372036.854775807},
10: {-922337203.6854775808, 922337203.6854775807},
11: {-92233720.36854775808, 92233720.36854775807},
12: {-9223372.036854775808, 9223372.036854775807},
13: {-922337.2036854775808, 922337.2036854775807},
14: {-92233.72036854775808, 92233.72036854775807},
15: {-9223.372036854775808, 9223.372036854775807},
16: {-922.3372036854775808, 922.3372036854775807},
17: {-92.23372036854775808, 92.23372036854775807},
18: {-9.223372036854775808, 9.223372036854775807},
}
type BitWidth int
const (
BitWidth8 BitWidth = 8
BitWidth16 BitWidth = 16
BitWidth32 BitWidth = 32
BitWidth64 BitWidth = 64
)
var inttab = map[BitWidth]Rb{
BitWidth8: {-128, 127},
BitWidth16: {-32768, 32767},
BitWidth32: {-2147483648, 2147483647},
BitWidth64: {-9223372036854775808, 9223372036854775807},
}
var uinttab = map[BitWidth]Urb{
BitWidth8: {0, 255},
BitWidth16: {0, 65535},
BitWidth32: {0, 4294967295},
BitWidth64: {0, 18446744073709551615},
}
func pathstr(path []string) string {
var str string
for _, v := range path {
str += "/" + strings.Replace(url.QueryEscape(v), "+", "%20", -1)
}
return str
}
type Type interface {
Validate(ctx ValidateCtx, path []string, s string) error
Name() xml.Name
errors() []string
ytype()
// Strings (possibly other types) may have an empty string as the default.
// So, we need to explicitly note the presence / absence of a default
Default() (string, bool)
AllowedValues(ctxNode xutils.XpathNode, debug bool) (
allowedValues []string, err error)
}
func genErrorString(t Type) string {
var buf bytes.Buffer
errstrs := t.errors()
if len(errstrs) == 1 {
return "Must have value " + errstrs[0]
}
buf.WriteString("Must have one of the following values: ")
for i, estr := range errstrs {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(estr)
}
return buf.String()
}
type Status int
const (
Current = iota
Deprecated
Obsolete
)
func (s Status) String() string {
switch s {
case Current:
return "Current"
case Deprecated:
return "Deprecated"
case Obsolete:
return "Obsolete"
default:
panic(fmt.Errorf("Unexpected status value %d", s))
}
}
/* types */
type yrestrict struct {
}
func (yrestrict) errors() []string {
return nil
}
func (yrestrict) restriction() {}
/*
* 'def'ault can't be set directly for a type. Instead it's either set on
* a typedef or a leaf / choice statement. However it is set, it ends up
* being associated with the leaf / choice schema node via its attached
* type node, so that is the logical place within the schema to store it.
*/
type ytyp struct {
yrestrict
name xml.Name
def string
hasDefault bool
}
func newType(name xml.Name, def string, hasDef bool) ytyp {
return ytyp{
name: name,
def: def,
hasDefault: hasDef,
}
}
func (t *ytyp) Name() xml.Name {
return t.name
}
func (t *ytyp) AllowedValues(
ctxNode xutils.XpathNode,
debug bool,
) ([]string, error) {
return []string{}, nil
}
func (t *ytyp) Default() (string, bool) {
if t.hasDefault {
return t.def, true
}
return "", false
}
func (t *ytyp) String() string {
if t.name.Space != "" {
return t.name.Space + ":" + t.name.Local
}
return t.name.Local
}
func (ytyp) ytype() {}
type Binary interface {
Type
Length() *Length
isBinary()
}
type binary struct {
ytyp
len *Length
}
// Ensure that other schema types don't meet the interface
func (*binary) isBinary() {}
// Compile time check that the concrete type meets the interface
var _ Binary = (*binary)(nil)
func (b *binary) Length() *Length { return b.len }
func (b *binary) Validate(ctx ValidateCtx, path []string, s string) error {
return b.len.Validate(uint64(len(s)))
}
func NewBinary() Binary {
return &binary{}
}
type Boolean interface {
Type
isBoolean()
}
type boolean struct {
ytyp
}
// Ensure that other schema types don't meet the interface
func (*boolean) isBoolean() {}
// Compile time check that the concrete type meets the interface
var _ Boolean = (*boolean)(nil)
func (b *boolean) Validate(ctx ValidateCtx, path []string, s string) error {
if s == "true" || s == "false" {
return nil
}
return newInvalidValueError(path, genErrorString(b))
}
func (b *boolean) errors() []string {
return []string{"true", "false"}
}
func NewBoolean(
name xml.Name,
def string,
hasDef bool,
) Boolean {
return &boolean{ytyp: newType(name, def, hasDef)}
}
type Decimal64 interface {
Number
Fd() Fracdigit
Rbs() DrbSlice
isDecimal64()
}
type decimal64 struct {
ytyp
fd Fracdigit
rbs DrbSlice
msg string
appTag string
}
// Ensure that other schema types don't meet the interface
func (*decimal64) isDecimal64() {}
// Compile time check that the concrete type meets the interface
var _ Decimal64 = (*decimal64)(nil)
func (d *decimal64) Fd() Fracdigit { return d.fd }
func (d *decimal64) Rbs() DrbSlice { return d.rbs }
func (d *decimal64) Ranges() RangeBoundarySlicer { return d.rbs }
func (d *decimal64) Msg() string { return d.msg }
func (d *decimal64) AppTag() string { return d.appTag }
func (d *decimal64) BitWidth() BitWidth { return BitWidth64 }
func NewDecimal64(
name xml.Name,
fd Fracdigit,
rbs []Drb,
msg string,
appTag string,
def string,
hasDef bool,
) Decimal64 {
if rbs == nil {
rbs = make([]Drb, 0, 1)
if fd > 0 {
rbs = append(rbs, fdtab[fd])
}
}
if appTag == "" {
appTag = "range-violation"
}
return &decimal64{
ytyp: newType(name, def, hasDef),
rbs: rbs,
msg: msg,
appTag: appTag,
fd: fd,
}
}
func (d *decimal64) Validate(ctx ValidateCtx, path []string, s string) error {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
goto out
}
err = validateDecimal64String(s, int(d.fd))
if err != nil {
goto out
}
if len(d.rbs) == 0 {
rb, ok := fdtab[d.fd]
if ok {
err = rb.Validate(f)
if err != nil {
goto out
}
}
goto out
}
for _, r := range d.rbs {
err = r.Validate(f)
if err == nil {
break
}
}
out:
if err == nil {
return nil
}
if d.msg != "" {
return newInvalidValueErrorWithAppTag(path, d.msg, d.appTag)
}
switch v := err.(type) {
case *strconv.NumError:
if v.Err == strconv.ErrSyntax {
return newInvalidValueErrorWithAppTag(path,
fmt.Sprintf("%s is not a decimal64", s), d.appTag)
}
return newInvalidValueErrorWithAppTag(path, genErrorString(d), d.appTag)
case *validateDecimal64Error:
return newInvalidValueErrorWithAppTag(path, err.Error(), d.appTag)
default:
return newInvalidValueErrorWithAppTag(path, genErrorString(d), d.appTag)
}
}
func (d *decimal64) errors() []string {
out := make([]string, 0, len(d.rbs))
for _, rb := range d.rbs {
out = append(out, rb.Error())
}
return out
}
type Empty interface {
Type
isEmpty()
}
type empty struct {
ytyp
}
// Ensure that other schema types don't meet the interface
func (*empty) isEmpty() {}
// Compile time check that the concrete type meets the interface
var _ Empty = (*empty)(nil)
func (*empty) Validate(ctx ValidateCtx, path []string, s string) error {
if s != "" {
if len(path) > 1 {
return NewEmptyLeafValueError(s, path[:len(path)-1])
}
return NewEmptyLeafValueError(s, []string{})
}
return nil
}
func NewEmpty(name xml.Name, def string, hasDef bool) Empty {
return &empty{ytyp: newType(name, def, hasDef)}
}
type Enumeration interface {
Type
Enums() []*Enum
String() string
isEnumeration()
}
type enumeration struct {
ytyp
enums []*Enum
}
// Ensure that other schema types don't meet the interface
func (*enumeration) isEnumeration() {}
// Compile time check that the concrete type meets the interface
var _ Enumeration = (*enumeration)(nil)
func (e *enumeration) Enums() []*Enum { return e.enums }
func (e *enumeration) String() string {
var s string
s = e.enums[0].Val
for _, e := range e.enums[1:] {
s = s + ", " + e.Val
}
return s
}
func (e *enumeration) Validate(ctx ValidateCtx, path []string, s string) error {
for _, en := range e.enums {
if en.Val == s {
return nil
}
}
return newInvalidValueError(path, genErrorString(e))
}
func (e *enumeration) errors() []string {
out := make([]string, 0, len(e.enums))
for _, enum := range e.enums {
if enum.Status() == Obsolete {
continue
}
out = append(out, enum.Val)
}
return out
}
func NewEnumeration(
name xml.Name,
enums []*Enum,
def string,
hasDef bool,
) Enumeration {
if enums == nil {
enums = make([]*Enum, 0)
}
return &enumeration{
ytyp: newType(name, def, hasDef),
enums: enums,
}
}
type Number interface {
Type
Ranges() RangeBoundarySlicer
Msg() string
AppTag() string
BitWidth() BitWidth
}
type Integer interface {
Number
Rbs() RbSlice
isInteger()
}
type integer struct {
ytyp
t BitWidth
rbs RbSlice
msg string
appTag string
}
// Ensure that other schema types don't meet the interface
func (*integer) isInteger() {}
// Compile time check that the concrete type meets the interface
var _ Integer = (*integer)(nil)
func (i *integer) Rbs() RbSlice { return i.rbs }
func (i *integer) Ranges() RangeBoundarySlicer { return i.rbs }
func (i *integer) Msg() string { return i.msg }
func (i *integer) AppTag() string { return i.appTag }
func (i *integer) BitWidth() BitWidth { return i.t }
func (i *integer) Validate(ctx ValidateCtx, path []string, s string) error {
var si int64
var e error
si, e = strconv.ParseInt(s, 10, int(i.t))
if e != nil {
goto out
}
if len(i.rbs) == 0 {
rb, ok := inttab[i.t]
if ok {
e = rb.Validate(si)
if e != nil {
goto out
}
}
goto out
}
for _, r := range i.rbs {
e = r.Validate(si)
if e == nil {
break
}
}
out:
if e == nil {
return nil
}
if i.msg != "" {
return newInvalidValueErrorWithAppTag(path, i.msg, i.appTag)
}
switch v := e.(type) {
case *strconv.NumError:
if v.Err == strconv.ErrSyntax {
return newInvalidValueErrorWithAppTag(path,
fmt.Sprintf("'%s' is not an int%d", s, i.t), i.appTag)
}
return newInvalidValueErrorWithAppTag(path, genErrorString(i), i.appTag)
default:
return newInvalidValueErrorWithAppTag(path, genErrorString(i), i.appTag)
}
}
func (i *integer) errors() []string {
out := make([]string, 0, len(i.Rbs()))
for _, rb := range i.Rbs() {
out = append(out, rb.Error())
}
return out
}
func NewInteger(
bitSize BitWidth,
name xml.Name,
rbs []Rb,
msg string,
appTag string,
def string,
hasDef bool,
) Integer {
if rbs == nil {
rbs = make([]Rb, 0, 1)
rbs = append(rbs, inttab[bitSize])
}
if appTag == "" {
appTag = "range-violation"
}
return &integer{
ytyp: newType(name, def, hasDef),
t: bitSize,
rbs: rbs,
msg: msg,
appTag: appTag,
}
}
type Uinteger interface {
Number
Rbs() UrbSlice
isUinteger()
}
type uinteger struct {
ytyp
t BitWidth
rbs UrbSlice
msg string
appTag string
}
// Ensure that other schema types don't meet the interface
func (*uinteger) isUinteger() {}
// Compile time check that the concrete type meets the interface
var _ Uinteger = (*uinteger)(nil)
func (i *uinteger) Rbs() UrbSlice { return i.rbs }
func (i *uinteger) Ranges() RangeBoundarySlicer { return i.rbs }
func (i *uinteger) Msg() string { return i.msg }
func (i *uinteger) AppTag() string { return i.appTag }
func (i *uinteger) BitWidth() BitWidth { return i.t }
func (i *uinteger) Validate(ctx ValidateCtx, path []string, s string) error {
var ui uint64
var e error
ui, e = strconv.ParseUint(s, 10, int(i.t))
if e != nil {
goto out
}
if len(i.rbs) == 0 {
rb, ok := uinttab[i.t]
if ok {
e = rb.Validate(ui)
if e != nil {
goto out
}
}
goto out
}
for _, r := range i.rbs {
e = r.Validate(ui)
if e == nil {
break
}
}
out:
if e == nil {
return nil
}
if i.Msg() != "" {
return newInvalidValueErrorWithAppTag(path, i.Msg(), i.AppTag())
}
switch v := e.(type) {
case *strconv.NumError:
if v.Err == strconv.ErrSyntax {
return newInvalidValueErrorWithAppTag(path,
fmt.Sprintf("'%s' is not an uint%d", s, i.t), i.AppTag())
}
return newInvalidValueErrorWithAppTag(path,
genErrorString(i), i.AppTag())
default:
return newInvalidValueErrorWithAppTag(path,
genErrorString(i), i.AppTag())
}
}
func (i *uinteger) errors() []string {
out := make([]string, 0, len(i.Rbs()))
for _, rb := range i.Rbs() {
out = append(out, rb.Error())
}
return out
}
func NewUinteger(
bitSize BitWidth,
name xml.Name,
rbs []Urb,
msg string,
appTag string,
def string,
hasDef bool,
) Uinteger {
if rbs == nil {
rbs = make([]Urb, 0, 1)
rbs = append(rbs, uinttab[bitSize])
}
if appTag == "" {
appTag = "range-violation"
}
return &uinteger{
ytyp: newType(name, def, hasDef),
t: bitSize,
rbs: rbs,
msg: msg,
appTag: appTag,
}
}
type String interface {
Type
Len() *Length
Pats() [][]Pattern
PatHelps() [][]string
isString()
}
type ystring struct {
ytyp
len *Length
pats [][]Pattern
pathelps [][]string
}
// Ensure that other schema types don't meet the interface
func (*ystring) isString() {}
// Compile time check that the concrete type meets the interface
var _ String = (*ystring)(nil)
func (s *ystring) Len() *Length { return s.len }
func (s *ystring) Pats() [][]Pattern { return s.pats }
func (s *ystring) PatHelps() [][]string { return s.pathelps }
func (y *ystring) Validate(ctx ValidateCtx, path []string, s string) error {
var err error
err = y.len.Validate(uint64(len(s)))
if err != nil {
switch merr := err.(type) {
case *mgmterror.InvalidValueApplicationError:
merr.Path = pathutil.Pathstr(path)
return merr
default:
return err
}
}
//patterns must contain all subtype patterns leading up to to this type
//every type can have or patterns. so we have a two demensional matrix of patterns.
for _, ps := range y.pats {
for _, p := range ps {
err = p.Validate(s)
if err != nil {
switch merr := err.(type) {
case *mgmterror.InvalidValueApplicationError:
merr.Path = pathutil.Pathstr(path)
return merr
default:
return err
}
}
}
}
return nil
}
func (y *ystring) errors() []string {
out := make([]string, 0, len(y.PatHelps()))
for _, pathelps := range y.PatHelps() {
for _, pathelp := range pathelps {
out = append(out, pathelp)
}
}
return out
}
func NewString(
name xml.Name,
pats [][]Pattern,
pathelps [][]string,
initlen *Length,
def string,
hasDef bool,
) String {
r := uinttab[BitWidth32]
if initlen == nil {
initlen = &Length{
Lbs: []Lb{
Lb{
Start: r.Start,
End: r.End,
},
},
}
}
if pats == nil {
pats = make([][]Pattern, 0)
}
if pathelps == nil {
pathelps = make([][]string, 0)
}
return &ystring{
ytyp: newType(name, def, hasDef),
pats: pats,
pathelps: pathelps,
len: initlen,
}
}
type Union interface {
Type
Typs() []Type
MatchType(ctx ValidateCtx, path []string, s string) Type
isUnion()
}
type union struct {
ytyp
typs []Type
}
// Ensure that other schema types don't meet the interface
func (*union) isUnion() {}
// Compile time check that the concrete type meets the interface
var _ Union = (*union)(nil)
func (u *union) Typs() []Type { return u.typs }
func (u *union) MatchType(ctx ValidateCtx, path []string, s string) Type {
for _, t := range u.typs {
err := t.Validate(ctx, path, s)
if err == nil {
if u, ok := t.(Union); ok {
// a type within a union matched
// get the non-union base type
return u.MatchType(ctx, path, s)
}
return t
}
}
return nil
}
func (u *union) Validate(ctx ValidateCtx, path []string, s string) error {
var err error
var matched bool
for _, t := range u.typs {
err = t.Validate(ctx, path, s)
if err == nil {
matched = true
break
}
}
if !matched {
return newInvalidValueError(path, genErrorString(u))
}
if err == nil {
return nil
}
return newInvalidValueError(path, genErrorString(u))
}
func (u *union) errors() []string {
var out []string
for _, t := range u.typs {
for _, estr := range t.errors() {
out = append(out, estr)
}
}
return out
}
func NewUnion(
name xml.Name,
typs []Type,
def string,
hasDef bool,
) Union {
if typs == nil {
typs = make([]Type, 0)
}
return &union{
ytyp: newType(name, def, hasDef),
typs: typs,
}
}
type Identityref interface {
Type
Identities() []*Identity
String() string
isIdentityRef()
}
type identityref struct {
ytyp
identities []*Identity
}
// Ensure that other schema types don't meet the interface
func (*identityref) isIdentityRef() {}
// Compile time check that the concrete type meets the interface
var _ Identityref = (*identityref)(nil)
func (i *identityref) Identities() []*Identity { return i.identities }
func (i *identityref) String() string {
var s string
s = i.identities[0].Val
for _, id := range i.identities[1:] {
s = s + ", " + id.Val
}
return s
}
func (i *identityref) Validate(ctx ValidateCtx, path []string, s string) error {
for _, id := range i.identities {
if id.Val == s {
return nil
}
}
return newInvalidValueError(path, genErrorString(i))
}
func (i *identityref) errors() []string {
out := make([]string, 0, len(i.identities))
for _, id := range i.identities {
if id.Status() == Obsolete {
continue
}
out = append(out, id.Val)
}
return out
}
func NewIdentityref(
name xml.Name,
ids []*Identity,
def string,
hasDef bool,
) Identityref {
if ids == nil {
ids = make([]*Identity, 0)
}
return &identityref{
ytyp: newType(name, def, hasDef),
identities: ids,
}
}
type InstanceId interface {
Type
Require() bool
isInstanceId()
}
type instanceId struct {
ytyp
require bool
}
// Ensure that other schema types don't meet the interface
func (*instanceId) isInstanceId() {}
// Compile time check that the concrete type meets the interface
var _ InstanceId = (*instanceId)(nil)
func (i *instanceId) Require() bool { return i.require }
func (i *instanceId) Validate(ctx ValidateCtx, path []string, s string) error {
// TODO Ensure valid XPATH with limited predicates
return nil
}
func NewInstanceId(
name xml.Name,
require bool,
def string,
hasDef bool,
) InstanceId {
return &instanceId{
ytyp: newType(name, def, hasDef),
require: require,
}
}
type Leafref interface {
Type
Mach() *xpath.Machine
GetAbsPath(xutils.PathType) xutils.PathType
isLeafref()
}
type leafref struct {
ytyp
mach *xpath.Machine
}
// Ensure that other schema types don't meet the interface
func (*leafref) isLeafref() {}
// Compile time check that the concrete type meets the interface
var _ Leafref = (*leafref)(nil)
func (l *leafref) Mach() *xpath.Machine { return l.mach }
func (i *leafref) Validate(ctx ValidateCtx, path []string, s string) error {
// Validation done at compile stage
return nil
}
func NewLeafref(
name xml.Name,
mach *xpath.Machine,
def string,
hasDef bool,
) Leafref {
return &leafref{
ytyp: newType(name, def, hasDef),
mach: mach,
}
}
func (lr *leafref) AllowedValues(
ctxNode xutils.XpathNode,
debug bool,
) (allowedValues []string, err error) {
return lr.mach.AllowedValues(ctxNode, debug)
}
// Get the absolute path pointed to by the leafref. Initially for pretty-
// printing, it may prove useful when validating leafref paths at compile
// time in due course ...
//