Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Commit

Permalink
Parse oneof before field, so that 'oneof' isn't considered a messag…
Browse files Browse the repository at this point in the history
…e-type in proto3. (#89)

Proto3 fields don't have a label qualifier (optional | repeated | required) to distinguish between those and 'oneof', so it was being treated as a field-type identifier.

Fixes #88
  • Loading branch information
jhugard authored May 10, 2018
1 parent ca65a0f commit adfe3d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
41 changes: 41 additions & 0 deletions Parser.Test/TestParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,17 @@ module Message =
|> should equal
<| TOneOf("MyOneof", [ TOneOfField("name",TString,1u,[("foo",TStrLit("bar"))]) ])

[<Fact>]
let ``Parse oneof with multiple cases`` () =
Parse.fromStringWithParser (ws >>. pOneOf) """
oneof test_oneof {
string name = 4;
int32 age = 9;
}
"""
|> should equal
<| TOneOf("test_oneof", [ TOneOfField("name",TString,4u,[]); TOneOfField("age",TInt32,9u,[]) ])

[<Fact>]
let ``Parse map`` () =
Parse.fromStringWithParser (ws >>. pMap) """
Expand Down Expand Up @@ -1043,3 +1054,33 @@ module Proto2 =
}
""" |> ignore
|> should not' (throw typeof<System.FormatException>)

[<Xunit.Trait("Kind", "Unit")>]
module RegressionTests =

[<Fact>]
let ``proto3 oneof type doesn't parse (#88)`` () =
System.Diagnostics.Debugger.Break()
Parse.fromStringWithParser pProto """
syntax = "proto3";
message SampleMessage {
oneof test_oneof {
string name = 4;
int32 age = 9;
}
}
"""
|> should equal (
[
TSyntax TProto3
TMessage ("SampleMessage",
[
TOneOf("test_oneof",
[
TOneOfField("name",TString,4u,[])
TOneOfField("age",TInt32,9u,[])
])

])
])
4 changes: 2 additions & 2 deletions Parser/Parser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ module Parse =
//let todoMsg = "Explicit 'optional' fields are prohibited in proto3 syntax. Since all fields are optional by default, the 'optional' label should simply be removed."
(opt (str_ws1 "repeated" >>% TRepeated)) |>> defArg TOptional

/// Parser for proto2 lable + ws
/// Parser for proto2 label + ws
let pLabel_ws1 = pLabel .>> ws1

/// Parser for types: double | int32 | etc...
Expand Down Expand Up @@ -481,13 +481,13 @@ module Parse =

choice [
(isProto2 >>. pGroup) // must be parsed first to avoid confusion
pOneOf // must be parsed before pField, so 'oneof' isn't considered a type in Proto3
pField
pMessageEnum
pMessageMessage
(isProto2 >>. pMessageExtend)
(isProto2 >>. pExtensions)
pMessageOption
pOneOf
pMap
pReserved
]
Expand Down

0 comments on commit adfe3d9

Please sign in to comment.