go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/starlarkproto/testdata/from_proto.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 # Simple fields and slices of simple fields. 19 m = proto.from_textpb(testprotos.SimpleFields, """ 20 i64: -64 21 i64_rep: 1 22 i64_rep: 2 23 i32: -32 24 ui64: 64 25 ui32: 32 26 b: true 27 f32: 2.0 28 f64: 3.0 29 s: "hello" 30 bs: "bytes" 31 bs_rep: "b0" 32 bs_rep: "b1" 33 """) 34 assert.eq(m.i64, -64) 35 assert.eq(list(m.i64_rep), [1, 2]) 36 assert.eq(m.i32, -32) 37 assert.eq(m.ui64, 64) 38 assert.eq(m.ui32, 32) 39 assert.eq(m.b, True) 40 assert.eq(m.f32, 2.0) 41 assert.eq(m.f64, 3.0) 42 assert.eq(m.s, "hello") 43 assert.eq(m.bs, "bytes") 44 assert.eq(list(m.bs_rep), ["b0", "b1"]) 45 46 # Enums. 47 m2 = proto.from_textpb(testprotos.Complex, "enum_val: ENUM_VAL_1") 48 assert.eq(m2.enum_val, testprotos.Complex.ENUM_VAL_1) 49 50 # Nested messages (singular and repeated). 51 m3 = proto.from_textpb(testprotos.MessageFields, """ 52 single: <i: 123> 53 rep: <i: 456> 54 rep: <i: 789> 55 """) 56 assert.eq(m3.single.i, 123) 57 assert.eq(len(m3.rep), 2) 58 assert.eq(m3.rep[0].i, 456) 59 assert.eq(m3.rep[1].i, 789) 60 61 # Oneofs. 62 m4 = proto.from_textpb(testprotos.Complex, "simple: <i: 123>") 63 assert.eq(m4.simple.i, 123) 64 assert.eq(m4.another_simple, None) 65 66 # Maps with primitive values. 67 m5 = proto.from_textpb(testprotos.MapWithPrimitiveType, """ 68 m1 < 69 key: "abc" 70 value: 1 71 > 72 m1 < 73 key: "def" 74 value: 2 75 > 76 """) 77 assert.eq(dict(m5.m1), {'abc': 1, 'def': 2}) 78 79 # Maps with message values. 80 m6 = proto.from_textpb(testprotos.MapWithMessageType, """ 81 m < 82 key: "abc" 83 value: <i: 1> 84 > 85 m < 86 key: "def" 87 value: <i: 2> 88 > 89 """) 90 assert.eq(len(m6.m), 2) 91 assert.eq(m6.m['abc'].i, 1) 92 assert.eq(m6.m['def'].i, 2)