git.delta.rocks / jrsonnet / refs/commits / de9f0cb0f75a

difftreelog

source

docs/features.adoc3.7 KiBrenderedsourcehistory
1= Features23Some features, which exists in jrsonnet, but not yet in other implementations.45Any of those features may be enabled during build time using feature flags, i.e: `--features=exp-destruct`.67== `exp-destruct`89Destructuring assignment, upstream issue: https://github.com/google/jsonnet/issues/3071011Destructuring object:1213[source,jsonnet]14----15local {a: b} = obj; ...16// Same as17local b = obj.a; ...18----1920Field name may be omitted:2122> However, field name omission looks off here, as currently jsonnet doesn't allows `local a = 1; {a}`23as a sugar for `local a = 1; {a: a}`, this causing asymmetry2425[source,jsonnet]26----27local {a} = obj; ...28// Same as29local a = obj.a; ...30----3132Rest of fields may be collected into another object:3334[source,jsonnet]35----36local {a, ...rest} = obj; ...37----3839And it is possible to set field defaults:4041[source,jsonnet]42----43local {a = 1} = {};4445a == 146----4748Destructuring arrays:4950[source,jsonnet]51----52local [a, b, c] = array; ...53----5455Rest of fields in any position may be collected into other array:5657[source,jsonnet]58----59local [...rest, a] = array; ...60local [a, ...rest] = array; ...61local [a, ...rest, b] = array; ...62----6364In case of not needed fields there is `?` (because `_` is not reserved):6566[source,jsonnet]67----68local [?, b, c] = ["a", "b", "c"]; ...69----7071Recursive destructuring also works:7273[source,jsonnet]74----75local {a: [{b: {c: d}}]} = {a:[{b:{c:5}}]}; d == 576----7778Also, mutually recursive declaration works:7980[source,jsonnet]81----82local83  {a, b, c} = {a: y, b: c, c: x},84  {x, y, z} = {x: a, y: 2, z: b};85z == 286----8788This feature also works in function arguments:89> It is impossible to reference those parameters using named argument syntax9091[source,jsonnet]92----93local myFun({a, b, c}) = a + b + c;9495myFun({a: 1, b: 2, c: 3})96----9798== `exp-preserve-order`99100Object field order preservation during manifestification, upstream issue: https://github.com/google/jsonnet/issues/903101102This feature adds a new CLI argument: `--preserve-order`,103as well as additional `std.manifest*/std.objectFields*` standard library functions argument `preserve_order`.104105Using this argument, it is possible to have same field order in manifestification, as in declaration:106107[source,jsonnet]108----109std.objectFields({c: 1, b: 2, a: 3}, preserve_order = false) == ['a', 'b', 'c'] # Fields were sorted110std.manifestJson({c: 1, b: 2, a: 3}, preserve_order = true) == ['c', 'b', 'a'] # Fields were serialized in declaration order111----112113== `exp-object-iteration`114115Iteration over object fields in comprehensions, upstream issue: https://github.com/google/jsonnet/issues/543116117This feature is not implemented as proposed in upstream, it only yields `[key, value]` arrays per element:118119[source,jsonnet]120----121{122    [i[0] + '!']: i[1] + '!'123    for i in {124        a: 1,125        b: 2,126        c: 3,127    }128} == {129    'a!': '1!',130    'b!': '2!',131    'c!': '3!',132}133----134135However, it may be combined with `exp-destruct`, to implement syntax close to proposed:136137[source,jsonnet]138----139{140    [k + '!']: v + '!'141    for [k, v] in {142        a: 1,143        b: 2,144        c: 3,145    }146} == {147    'a!': '1!',148    'b!': '2!',149    'c!': '3!',150}151----152153Unfortunately, there is no integration with the `exp-preserve-order` feature,154fields will still be iterated in sorted order, and using old syntax is required:155156[source,jsonnet]157----158local obj = {159    c: 3,160    b: 2,161    a: 1,162};163164{165    [key + '!']: obj[key] + '!'166    for key in std.objectFields(obj, preserve_order: true)167} == {168    'c!': '3!',169    'b!': '2!',170    'a!': '1!',171}172----173174== `exp-null-coalescing`175176Provides a new operator and new indexing syntax sugar:177178`a ?? b` - equivalent to `if a == null then b else a`179180`a?.b`, `a?.['b']` - equivalent to `if a != null then std.get(a, 'b', null)`