PField
#struct PFieldOne message field.
| Name | Type | Description |
|---|---|---|
name | str = "" | The name. |
kind | int = 0 | The field kind; one of the FK_ constants. |
typeName | str = "" | The type name for a message and an enum. The parser does not resolve names, so an enum comes out as a message kind too. |
keyKind | int = 0 | The key kind of a map. |
mapValueKind | int = 0 | The value kind of a map. |
mapValueTypeName | str = "" | The type name of the map value. |
number | int = 0 | The field number. |
repeated | bool = false | The field is repeated. |
packedOpt | bool = true | Repeated numbers are written packed. |
oneofIndex | int = -1 | The index of the oneof group; -1 when there is none. |
is_deprecated | bool = false | The field is marked deprecated. |
import encoding.protobuf
func main throw:
src := "syntax = \"proto3\";\npackage shop;\n\nenum Status { OPEN = 0; DONE = 1; }\n\nmessage Order {\n int64 id = 1;\n repeated string items = 2;\n map<string, int32> counts = 3;\n Status status = 4;\n oneof pay { string card = 5; bool cash = 6; }\n}\n\nservice Orders {\n rpc Get (Order) returns (Order);\n rpc Watch (Order) returns (stream Order);\n}\n"
p := try protobuf.ParseFile(src)
println p.packageName, p.messages.Len(), p.enums.Len(), p.services.Len(), p.imports.Len()
msg := p.messages[0]
println msg.name, msg.fields.Len(), msg.oneofs[0].name, msg.oneofs[0].fieldIndices
each f in msg.fields:
println f.number,
f.name,
protobuf.FieldKindName(f.kind),
f.typeName,
f.repeated,
f.oneofIndex
end
m := msg.fields[2]
println m.kind == protobuf.FK_MAP,
protobuf.FieldKindName(m.keyKind),
protobuf.FieldKindName(m.mapValueKind)
e := p.enums[0]
println e.name, e.values[1].name, e.values[1].number
s := p.services[0]
println s.name,
s.methods[1].name,
s.methods[1].inputType,
s.methods[1].serverStream,
s.methods[1].clientStream
println protobuf.ScalarKindFromName("sint64") == protobuf.FK_SINT64,
protobuf.ScalarKindFromName("Order")
mut toks := try protobuf.Tokenize("message A { int32 x = 1; }")
println toks.toks.Len(),
toks.toks[0].lexeme,
toks.toks[0].kind == protobuf.PTK_IDENT,
toks.toks[2].kind == protobuf.PTK_LBRACE
println toks.toks[6].ival, toks.toks[6].line, toks.toks[6].offset
try:
_ := protobuf.ParseFile("message {")
catch e:
println e.KindName(), e.line, e.column, e.code
println e.ToStr()
end
endshop 1 1 1 0
Order 6 pay [4, 5]
1 id int64 false -1
2 items string true -1
3 counts map false -1
4 status message Status false -1
5 card string false 0
6 cash bool false 0
true string int32
Status DONE 1
Orders Watch Order true false
true 0
10 message true true
1 1 22
syntax error 1 9 protobuf.unexpected_token
protobuf: expected a message name, found '{' (line 1, column 9)