this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/jsonschema: use more generic `siblings` function

We want to be able to merge `anyOf` (and potentially `oneOf`)
items as well as `allOf`, so refactor `conjuncts` into `siblings`
which can do all the above.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ia7aaed4b85e46a38aaba455033d4093cf6ebd160
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1224301
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+32 -7
+11 -7
encoding/jsonschema/generate.go
··· 122 122 elems: make([]item, 0, len(it.elems)), 123 123 } 124 124 loop: 125 - for e := range conjuncts(it) { 125 + for e := range siblings(it) { 126 126 // Remove elements that are entirely redundant. 127 127 // Note: DeepEqual seems reasonable here because values are generally 128 128 // small and the data structures are well-defined. We could ··· 145 145 } 146 146 } 147 147 148 - func conjuncts(it *itemAllOf) iter.Seq[item] { 148 + type elementsItem interface { 149 + elements() []item 150 + } 151 + 152 + func siblings[T elementsItem](it T) iter.Seq[item] { 149 153 return func(yield func(item) bool) { 150 - yieldConjuncts(it, yield) 154 + yieldSiblings(it, yield) 151 155 } 152 156 } 153 157 154 - func yieldConjuncts(it *itemAllOf, yield func(item) bool) bool { 155 - for _, e := range it.elems { 156 - if ae, ok := e.(*itemAllOf); ok { 157 - if !yieldConjuncts(ae, yield) { 158 + func yieldSiblings[T elementsItem](it T, yield func(item) bool) bool { 159 + for _, e := range it.elements() { 160 + if ae, ok := e.(T); ok { 161 + if !yieldSiblings(ae, yield) { 158 162 return false 159 163 } 160 164 } else {
+21
encoding/jsonschema/generate_items.go
··· 70 70 i.elems = append(i.elems, it) 71 71 } 72 72 73 + var _ elementsItem = (*itemAllOf)(nil) 74 + 75 + // elements implements [elementsItem]. 76 + func (i *itemAllOf) elements() []item { 77 + return i.elems 78 + } 79 + 73 80 func (i *itemAllOf) generate(g *generator) ast.Expr { 74 81 // Because a single json schema object is essentially an allOf itself, 75 82 // we can merge objects that don't share keywords ··· 169 176 return &itemOneOf{elems: elems} 170 177 } 171 178 179 + var _ elementsItem = (*itemOneOf)(nil) 180 + 181 + // elements implements [elementsItem]. 182 + func (i *itemOneOf) elements() []item { 183 + return i.elems 184 + } 185 + 172 186 // itemAnyOf represents an anyOf combinator 173 187 type itemAnyOf struct { 174 188 elems []item ··· 184 198 return i 185 199 } 186 200 return &itemAnyOf{elems: elems} 201 + } 202 + 203 + var _ elementsItem = (*itemOneOf)(nil) 204 + 205 + // elements implements [elementsItem]. 206 + func (i *itemAnyOf) elements() []item { 207 + return i.elems 187 208 } 188 209 189 210 // itemNot represents a not combinator