this repo has no description
0
fork

Configure Feed

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

encoding/jsonschema: recognize builtin validators without call

Currently the encoding/jsonschema generate logic does not
have any way of recognizing builtin validators unless they're
in a call expression. However, CUE does not require them to
be called (`list.UniqueItems` is as valid as `list.UniqueItems()`)
so fix that behavior.

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

+24 -19
+13 -4
encoding/jsonschema/generate.go
··· 291 291 if !pkg.Exists() { 292 292 break 293 293 } 294 + // Check if this is a reference to a known validator function. 295 + // For example, list.UniqueItems (without parens) should be treated 296 + // the same as list.UniqueItems(). 297 + if it := g.makeCallItem(v, []cue.Value{v}, mode); it != nil { 298 + return it 299 + } 294 300 // It's a reference: generate a definition for it. 295 301 // TODO Not all references need or should have a definition. 296 302 if name := g.cfg.NameFunc(pkg, path); name != "" { ··· 422 428 return &itemFalse{} 423 429 } 424 430 case cue.CallOp: 425 - return g.makeCallItem(v, args, mode) 431 + if it := g.makeCallItem(v, args, mode); it != nil { 432 + return it 433 + } 434 + // For unknown functions, accept anything rather than fail. 435 + // This allows for gradual implementation of more function types. 436 + return &itemTrue{} 426 437 } 427 438 if !v.IsNull() { 428 439 // We want to encode null as {type: "null"} not {const: null} ··· 864 875 } 865 876 866 877 default: 867 - // For unknown functions, accept anything rather than fail. 868 - // This allows for gradual implementation of more function types 869 - return &itemTrue{} 878 + return nil 870 879 } 871 880 } 872 881
+11 -15
encoding/jsonschema/testdata/generate/uniqueitems.txtar
··· 7 7 8 8 uniqueListNoCall?: list.UniqueItems 9 9 10 - // TODO this doesn't work - the code currently just looks 11 - // for call expressions but this isn't a call expression. 12 10 uniqueBools?: list.UniqueItems() & [bool, bool] 13 11 14 12 -- datatest/tests.cue -- ··· 27 25 28 26 badUniqueNoCall: { 29 27 data: uniqueListNoCall: [1, 1, 2] 30 - // TODO this should be an error. 31 - //error: true 28 + error: true 32 29 } 33 30 34 31 badUniqueBools: { ··· 38 35 -- out/generate-v3/schema -- 39 36 { 40 37 $schema: "https://json-schema.org/draft/2020-12/schema" 41 - $defs: { 42 - UniqueItems: { 43 - type: "array" 44 - } 45 - } 46 - type: "object" 38 + type: "object" 47 39 properties: { 48 40 uniqueBools: { 49 41 type: "array" ··· 61 53 uniqueItems: true 62 54 } 63 55 uniqueListNoCall: { 64 - $ref: "#/$defs/UniqueItems" 56 + type: "array" 57 + uniqueItems: true 65 58 } 66 59 } 67 60 } 68 61 -- out/generate-v3/ok -- 69 62 -- out/generate-v3/badUnique -- 70 63 badUnique.data.uniqueList: invalid value [1,1,2] (does not satisfy list.UniqueItems): equal value (1) at position 0 and 1: 71 - 1:289 64 + 1:248 72 65 ./datatest/tests.cue:10:20 73 66 -- out/generate-v3/badUniqueBools -- 74 67 badUniqueBools.data.uniqueBools: invalid value [true,true] (does not satisfy list.UniqueItems): equal value (true) at position 0 and 1: 75 - 1:240 76 - ./datatest/tests.cue:21:21 77 - 1:214 68 + 1:199 69 + ./datatest/tests.cue:20:21 70 + 1:173 78 71 -- out/generate-v3/badUniqueNoCall -- 72 + badUniqueNoCall.data.uniqueListNoCall: invalid value [1,1,2] (does not satisfy list.UniqueItems): equal value (1) at position 0 and 1: 73 + 1:303 74 + ./datatest/tests.cue:15:26