go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/from_dict.star (about)

     1  # Copyright 2019 The LUCI 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  l = proto.new_loader(proto.new_descriptor_set(blob=read('./testprotos/all.pb')))
    16  testprotos = l.module('go.chromium.org/luci/starlark/starlarkproto/testprotos/test.proto')
    17  
    18  def from_dict(cls, d):
    19    return cls(**d)
    20  
    21  # Works.
    22  m1 = from_dict(testprotos.MessageFields, {
    23    'single': {'i': 123},
    24    'rep': [{'i': 456}, {'i': 789}, None, testprotos.Simple(i=999)],
    25  })
    26  assert.eq(m1.single.i, 123)
    27  assert.eq(type(m1.rep), 'list<proto.Message<testprotos.Simple>>')
    28  assert.eq(len(m1.rep), 4)
    29  assert.eq(m1.rep[0].i, 456)
    30  assert.eq(m1.rep[1].i, 789)
    31  assert.eq(m1.rep[2].i, 0)   # fills in Nones with default values
    32  assert.eq(m1.rep[3].i, 999)
    33  
    34  # All Nones are converted to list of default values.
    35  m2 = from_dict(testprotos.MessageFields, {
    36    'rep': [None, None],
    37  })
    38  assert.eq(len(m2.rep), 2)
    39  assert.eq(m2.rep[0].i, 0)
    40  assert.eq(m2.rep[1].i, 0)
    41  
    42  # Tuples work too.
    43  m3 = from_dict(testprotos.MessageFields, {
    44    'rep': ({'i': 456},),
    45  })
    46  assert.eq(type(m3.rep), 'list<proto.Message<testprotos.Simple>>')  # converted to a list
    47  assert.eq(len(m3.rep), 1)
    48  assert.eq(m3.rep[0].i, 456)
    49  
    50  # For oneof fields the last one wins (note that Starlark dicts are ordered).
    51  m4 = from_dict(testprotos.Complex, {
    52    'simple': {'i': 1},
    53    'another_simple': {'j': 2},
    54  })
    55  assert.eq(m4.simple, None)
    56  assert.eq(m4.another_simple.j, 2)
    57  
    58  # Fails on wrong schema (singular field).
    59  def wrong_schema_single():
    60    from_dict(testprotos.MessageFields, {
    61      'single': {'z': '???'},
    62    })
    63  assert.fails(wrong_schema_single, 'proto.Message<testprotos.Simple> has no field "z"')
    64  
    65  # Fails on wrong schema (repeated field).
    66  def wrong_schema_repeated():
    67    from_dict(testprotos.MessageFields, {
    68      'rep': [{'z': '???'}],
    69    })
    70  assert.fails(wrong_schema_repeated, 'item #0: proto.Message<testprotos.Simple> has no field "z"')
    71  
    72  # Fails on non-string keys.
    73  def bad_key_type():
    74    from_dict(testprotos.MessageFields, {
    75      'single': {123: 1},
    76    })
    77  assert.fails(bad_key_type, 'got int dict key, want string')