go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/imported_msg.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  
    18  m = testprotos.RefsOtherProtos()
    19  
    20  # Indirectly using messages imported from elsewhere works.
    21  m.another_msg.i = 123
    22  m.ts.seconds = 456
    23  assert.eq(proto.to_textpb(m), """another_msg {
    24    i: 123
    25  }
    26  ts {
    27    seconds: 456
    28  }
    29  """)
    30  
    31  # To instantiate them directly, need to load the corresponding proto module
    32  # first. To avoid clashing with already imported 'testprotos' symbol, import it
    33  # under a different name.
    34  another_pb = l.module('go.chromium.org/luci/starlark/starlarkproto/testprotos/another.proto')
    35  assert.eq(proto.to_textpb(another_pb.AnotherMessage(i=123)), "i: 123\n")
    36  
    37  # Can import modules with dotted names too.
    38  google_pb = l.module('google/protobuf/timestamp.proto')
    39  assert.eq(proto.to_textpb(google_pb.Timestamp(seconds=456)), "seconds: 456\n")