cuelang.org/go@v0.10.1/internal/pkg/types.go (about) 1 // Copyright 2022 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package pkg 16 17 import ( 18 "cuelang.org/go/internal/core/adt" 19 ) 20 21 // List represents a CUE list, which can be open or closed. 22 type List struct { 23 runtime adt.Runtime 24 node *adt.Vertex 25 isOpen bool 26 } 27 28 // Elems returns the elements of a list. 29 func (l *List) Elems() []*adt.Vertex { 30 return l.node.Elems() 31 } 32 33 // IsOpen reports whether a list is open ended. 34 func (l *List) IsOpen() bool { 35 return l.isOpen 36 } 37 38 // Struct represents a CUE struct, which can be open or closed. 39 type Struct struct { 40 runtime adt.Runtime 41 node *adt.Vertex 42 } 43 44 // Arcs returns all arcs of s. 45 func (s *Struct) Arcs() []*adt.Vertex { 46 return s.node.Arcs 47 } 48 49 // Len reports the number of regular string fields of s. 50 func (s *Struct) Len() int { 51 count := 0 52 for _, a := range s.Arcs() { 53 if a.Label.IsString() && !s.node.IsOptional(a.Label) { 54 count++ 55 } 56 } 57 return count 58 } 59 60 // IsOpen reports whether s allows more fields than are currently defined. 61 func (s *Struct) IsOpen() bool { 62 if !s.node.IsClosedStruct() { 63 return true 64 } 65 // Technically this is not correct, but it is in the context of where 66 // it is used. 67 if s.node.PatternConstraints != nil && len(s.node.PatternConstraints.Pairs) > 0 { 68 return true 69 } 70 // The equivalent code for the old implementation. 71 ot := s.node.OptionalTypes() 72 if ot&^adt.HasDynamic != 0 { 73 return true 74 } 75 return false 76 } 77 78 // A ValidationError indicates an error that is only valid if a builtin is used 79 // as a validator. 80 type ValidationError struct { 81 B *adt.Bottom 82 } 83 84 func (v ValidationError) Error() string { return v.B.Err.Error() }