go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/wire.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  # Can round-trip through binary encoding.
    19  msg = testprotos.Complex(
    20      enum_val = 1,
    21      i64 = 123,
    22      i64_rep = [1, 2, 3],
    23      mp = {'1': {}, '2': {}},
    24      msg_val = {'i': 123},
    25      msg_val_rep = [{}, {}, {'i': 123}, {}],
    26  )
    27  blob = proto.to_wirepb(msg)
    28  assert.eq(len(blob), 37)
    29  assert.eq(msg, proto.from_wirepb(testprotos.Complex, blob))
    30  
    31  # Maps are serialized deterministically.
    32  mapMsg = testprotos.MapWithPrimitiveType(m3={i: i for i in range(100)})
    33  b1 = proto.to_wirepb(mapMsg)
    34  b2 = proto.to_wirepb(mapMsg)
    35  assert.eq(b1, b2)
    36  
    37  # The returned message is frozen.
    38  def from_wirepb_frozen():
    39    proto.from_wirepb(testprotos.Complex, blob).i64 = 456
    40  assert.fails(from_wirepb_frozen, 'cannot modify frozen')