美文网首页
jsonnet入门

jsonnet入门

作者: wwq2020 | 来源:发表于2020-07-23 23:28 被阅读0次

jsonnet 是 Google 推出的一门 JSON 模板语言.

编译器

定义变量

编写 demo.jsonnet,内容如下

local data = 1;

{
  data: data,
}

执行

jsonnet demo.jsonnet > demo.json

输出

{
   "data": 1
}

引用

self 指向当前对象
$指向最外部对象
["name"]查找 field
.name 如果 name 是标识符
[1] 查找数组元素

local array = [1,2,3];
{
   "data1": array[1],
   "data2": self.data1,
   "data3": {
       data: $.data1,
   },
   "data4": {
       data: $["data3"].data,
   },
}

执行

jsonnet demo.jsonnet > demo.json

输出

local array = [1,2,3];
{
   "data1": array[1],
   "data2": self.data1,
   "data3": {
       data: $.data1,
   },
   "data4": {
       data: $["data3"].data,
   },
}

算术

编写 demo.jsonnet,内容如下

{
  concat_array: [1, 2, 3] + [4],
  concat_string: '123' + 4,
  equality1: 1 == '1',
  equality2: [{}, { x: 3 - 1 }]
             == [{}, { x: 2 }],
  ex1: 1 + 2 * 3 / (4 + 5),
  // 位操作,首先转为int
  ex2: self.ex1 | 3,
  // 取模
  ex3: self.ex1 % 2,
  // 逻辑操作
  // Boolean logic
  ex4: (4 > 3) && (1 <= 3) || false,
  // 对象混合
  // Mixing objects together
  obj: { a: 1, b: 2 } + { b: 3, c: 4 },
  // 测试field是否在对象内
  obj_member: 'foo' in { foo: 1 },
  // 字符串格式化
  str1: 'The value of self.ex2 is '
        + self.ex2 + '.',
  str2: 'The value of self.ex2 is %g.'
        % self.ex2,
  str3: 'ex1=%0.2f, ex2=%0.2f'
        % [self.ex1, self.ex2],
  // 传入self,ex1和ex2内部提取
  str4: 'ex1=%(ex1)0.2f, ex2=%(ex2)0.2f'
        % self,
  // 对整个文件进行文本模板化
  str5: |||
    ex1=%(ex1)0.2f
    ex2=%(ex2)0.2f
  ||| % self,
}

执行

jsonnet demo.jsonnet > demo.json

输出

{
  "concat_array": [
    1,
    2,
    3,
    4
  ],
  "concat_string": "1234",
  "equality1": false,
  "equality2": true,
  "ex1": 1.6666666666666665,
  "ex2": 3,
  "ex3": 1.6666666666666665,
  "ex4": true,
  "obj": {
    "a": 1,
    "b": 3,
    "c": 4
  },
  "obj_member": true,
  "str1": "The value of self.ex2 is 3.",
  "str2": "The value of self.ex2 is 3.",
  "str3": "ex1=1.67, ex2=3.00",
  "str4": "ex1=1.67, ex2=3.00",
  "str5": "ex1=1.67\nex2=3.00\n"
}

函数

编写 demo.jsonnet,内容如下

local demo(x, y=1) = x + y;
{
  data: demo(1),
}

执行

jsonnet demo.jsonnet > demo.json

输出

{
   "data": 2
}

条件

编写 demo.jsonnet,内容如下

local demo(flag) =
  if flag then 'hello' else 'world';

{
  data1: demo(true),
  data2: demo(false),
}

执行

jsonnet demo.jsonnet > demo.json

输出

{
   "data1": "hello",
   "data2": "world"
}

数组和对象

编写 demo.jsonnet,内容如下

local arr = std.range(5, 8);
{
  array_comprehensions: {
    higher: [x + 3 for x in arr],
    lower: [x - 3 for x in arr],
    evens: [x for x in arr if x % 2 == 0],
    odds: [x for x in arr if x % 2 == 1],
    evens_and_odds: [
      '%d-%d' % [x, y]
      for x in arr
      if x % 2 == 0
      for y in arr
      if y % 2 == 1
    ],
  },
  object_comprehensions: {
    evens: {
      ['f' + x]: true
      for x in arr
      if x % 2 == 0
    },
    // Use object composition (+) to add in
    // static fields:
    mixture: {
      f: 1,
      g: 2,
    } + {
      [x]: 0
      for x in ['a', 'b', 'c']
    },
  },
}

执行

jsonnet demo.jsonnet > demo.json

输出

{
  "cocktails": {
    "Bee's Knees": {
      "garnish": "Lemon Twist",
      "ingredients": [
        {
          "kind": "Honey Syrup",
          "qty": 1.3333333333333333
        },
        {
          "kind": "Lemon Juice",
          "qty": 1.3333333333333333
        },
        {
          "kind": "Farmers Gin",
          "qty": 1.3333333333333333
        }
      ],
      "served": "Straight Up"
    },
    "Screwdriver": {
      "ingredients": [
        {
          "kind": "Vodka",
          "qty": 1.5
        },
        {
          "kind": "Orange Juice",
          "qty": 3
        }
      ],
      "served": "On The Rocks"
    },
    "Yellow Screwdriver": {
      "ingredients": [
        {
          "kind": "Vodka",
          "qty": 1.5
        },
        {
          "kind": "Lemonade",
          "qty": 3
        }
      ],
      "served": "On The Rocks"
    }
  }
}

导入

编写 demo1.libsonnet,内容如下

local data = 1;

{
  data: data,
}

编写 demo2.jsonnet,内容如下

local demo1 = import 'demo1.libsonnet';

{
  data: demo1.data + 2,
}

执行

jsonnet demo2.jsonnet > demo2.json

输出

{
   "data": 3
}

错误

编写 demo.jsonnet,内容如下

// Extend above example to sanity check input.
local equal_parts(size, ingredients) =
  local qty = size / std.length(ingredients);
  // Check a pre-condition
  if std.length(ingredients) == 0 then
    error 'Empty ingredients.'
  else [
    { kind: i, qty: qty }
    for i in ingredients
  ];

local subtract(a, b) =
  assert a > b : 'a must be bigger than b';
  a - b;

assert std.isFunction(subtract);

{
  test1: equal_parts(1, ['Whiskey']),
  test2: subtract(10, 3),
  object: {
    assert self.f < self.g : 'wat',
    f: 1,
    g: 2,
  },
  assert std.isObject(self.object),
}

执行

jsonnet demo2.jsonnet > demo2.json

输出

{
  "object": {
    "f": 1,
    "g": 2
  },
  "test1": [
    {
      "kind": "Whiskey",
      "qty": 1
    }
  ],
  "test2": 7
}

外部变量

编写 demo.jsonnet,内容如下

{
  [std.extVar('prefix') + 'Pina Colada']: {
    ingredients: [
      { kind: 'Rum', qty: 3 },
      { kind: 'Pineapple Juice', qty: 6 },
      { kind: 'Coconut Cream', qty: 2 },
      { kind: 'Ice', qty: 12 },
    ],
    garnish: 'Pineapple slice',
    served: 'Frozen',
  },

  [if std.extVar('brunch') then
    std.extVar('prefix') + 'Bloody Mary'
  ]: {
    ingredients: [
      { kind: 'Vodka', qty: 1.5 },
      { kind: 'Tomato Juice', qty: 3 },
      { kind: 'Lemon Juice', qty: 1.5 },
      { kind: 'Worcestershire', qty: 0.25 },
      { kind: 'Tobasco Sauce', qty: 0.15 },
    ],
    garnish: 'Celery salt & pepper',
    served: 'Tall',
  },
}

执行

jsonnet demo2.jsonnet > demo2.json

输出

{
   "Happy Hour Bloody Mary": {
      "garnish": "Celery salt & pepper",
      "ingredients": [
         {
            "kind": "Vodka",
            "qty": 1.5
         },
         {
            "kind": "Tomato Juice",
            "qty": 3
         },
         {
            "kind": "Lemon Juice",
            "qty": 1.5
         },
         {
            "kind": "Worcestershire",
            "qty": 0.25
         },
         {
            "kind": "Tobasco Sauce",
            "qty": 0.14999999999999999
         }
      ],
      "served": "Tall"
   },
   "Happy Hour Pina Colada": {
      "garnish": "Pineapple slice",
      "ingredients": [
         {
            "kind": "Rum",
            "qty": 3
         },
         {
            "kind": "Pineapple Juice",
            "qty": 6
         },
         {
            "kind": "Coconut Cream",
            "qty": 2
         },
         {
            "kind": "Ice",
            "qty": 12
         }
      ],
      "served": "Frozen"
   }
}

面向对象

编写 demo.jsonnet,内容如下

local Base = {
  f: 2,
  g: self.f + 100,
};

local WrapperBase = {
  Base: Base,
};

{
  Derived: Base + {
    f: 5,
    old_f: super.f,
    old_g: super.g,
  },
  WrapperDerived: WrapperBase + {
    Base+: { f: 5 },
  },
}

执行

jsonnet demo2.jsonnet > demo2.json

输出

{
  "Derived": {
    "f": 5,
    "g": 105,
    "old_f": 2,
    "old_g": 105
  },
  "WrapperDerived": {
    "Base": {
      "f": 5,
      "g": 105
    }
  }
}

相关文章

网友评论

      本文标题:jsonnet入门

      本文链接:https://www.haomeiwen.com/subject/zioklktx.html