alexeyxo / protobuf-swift

Google ProtocolBuffers for Apple Swift
http://protobuf.io/#swift
Apache License 2.0
938 stars 138 forks source link

Problem escaping 'static' and 'operator' #181

Closed gmckenzi closed 8 years ago

gmckenzi commented 8 years ago

Version of protoc (protoc --version)

3.0

Version of ProtocolBuffers.framework

3.0.0

.proto file to reproduce

syntax = "proto3";
package Test;

enum Operator
{
    OperatorA  = 0;
    OperatorB  = 1;
    OperatorC  = 2;
}

message Value 
{
    bool nil = 1;
    Operator operator = 2;
}

message Expression
{
    repeated Operator code = 1;
    Value static = 2;
}

Description

I have modified the kKeywordList in swift_helpers.cc to include support for escaping the keywords nil and operator. It would be great if nil and operator were already part of the list. The keyword static was already present in the list.

After generating Swift code, there are a couple of places where the escaping of the keywords fails.

Here is the operator issue. Note the unescaped operator.toString() on line 11.

01    override public func encode() throws -> Dictionary<String,Any> {
02      guard isInitialized() else {
03        throw ProtocolBuffersError.invalidProtocolBuffer("Uninitialized Message")
04      }
05
06     var jsonMap:Dictionary<String,Any> = Dictionary<String,Any>()
07      if hasNil {
08        jsonMap["nil"] = `nil`
09      }
10      if hasOperator {
11        jsonMap["operator"] = operator.toString()
12      }
13      return jsonMap
14    }

And here is the static issue. Note the unescaped if static != nil on line 5.

01      public func getStaticBuilder() -> Test.Value.Builder {
02        if staticBuilder_ == nil {
03           staticBuilder_ = Test.Value.Builder()
04           builderResult.`static` = staticBuilder_.getMessage()
05           if static != nil {
06              try! staticBuilder_.mergeFrom(other: `static`)
07           }
08        }
09        return staticBuilder_
10      }
alexeyxo commented 8 years ago

Done. Thanks!

gmckenzi commented 8 years ago

Excellent! Thank you. That addressed the static scenario great, but the operator scenario is still occurring.

alexeyxo commented 8 years ago

Now, will be ok.