go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/has.star (about) 1 # Copyright 2020 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.Complex() 19 20 # Completely unknown fields are unset. 21 assert.true(not proto.has(m, 'doesnt_exist')) 22 23 24 # Scalar fields with default values are assumed to be always set. With proto3 25 # there's no way to distinguish a field initialized with its default value from 26 # an unset field. 27 assert.true(proto.has(m, 'i64')) 28 assert.true(proto.has(m, 'enum_val')) 29 30 31 # Repeated fields are assumed to be always set as well (they just may be empty). 32 assert.true(proto.has(m, 'i64_rep')) 33 assert.true(proto.has(m, 'msg_val_rep')) 34 35 36 # Singular message-typed fields can be uninitialized. 37 assert.true(not proto.has(m, 'msg_val')) 38 m.msg_val = {} 39 assert.true(proto.has(m, 'msg_val')) 40 m.msg_val = None 41 assert.true(not proto.has(m, 'msg_val')) 42 43 44 # One-of alternatives behave as message-typed fields, even if they have 45 # a primitive type. 46 assert.true(not proto.has(m, 'simple')) 47 assert.true(not proto.has(m, 'i64_oneof')) 48 49 m.simple = {} 50 assert.true(proto.has(m, 'simple')) 51 assert.true(not proto.has(m, 'i64_oneof')) 52 53 m.i64_oneof = 0 54 assert.true(not proto.has(m, 'simple')) 55 assert.true(proto.has(m, 'i64_oneof')) 56 57 m.i64_oneof = None 58 assert.true(not proto.has(m, 'simple')) 59 assert.true(not proto.has(m, 'i64_oneof'))