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

     1  # Copyright 2018 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  proto2 = l.module('go.chromium.org/luci/starlark/starlarkproto/testprotos/proto2.proto')
    18  
    19  m = proto2.Proto2Message()
    20  
    21  # Scalar fields.
    22  assert.eq(m.i, 0)
    23  m.i = 123
    24  assert.eq(m.i, 123)
    25  
    26  # Repeated fields.
    27  assert.eq(len(m.rep_i), 0)
    28  m.rep_i = [1, 2, 3]
    29  assert.eq(list(m.rep_i), [1, 2, 3])
    30  
    31  # Serialization works.
    32  assert.eq(proto.to_textpb(m), "i: 123\nrep_i: 1\nrep_i: 2\nrep_i: 3\n")