Skip to content
Browse files

Fix generation of enums using implicit values

If some or all of the values in an enum are not explicitly numbered, they should count up from the last explicitly numbered value (or from zero if no explicit value was seen yet).
  • Loading branch information...
1 parent 6913818 commit 0bbd0d95d23138a209766db4e424625667b5c38c @andrewcox andrewcox committed Feb 1, 2013
View
3 ...nerator/src/main/java/com/facebook/swift/generator/template/TemplateContextGenerator.java
@@ -131,8 +131,7 @@ public EnumContext enumFromThrift(final StringEnum stringEnum)
public EnumFieldContext fieldFromThrift(final IntegerEnumField field)
{
- Preconditions.checkState(field.getValue().get() != null, "field value for integer field %s is null!", field.getName());
- return new EnumFieldContext(mangleJavaConstantName(field.getName()), field.getValue().get());
+ return new EnumFieldContext(mangleJavaConstantName(field.getName()), field.getValue());
}
public EnumFieldContext fieldFromThrift(final String value)
View
11 swift-idl-parser/src/main/antlr3/com/facebook/swift/parser/antlr/DocumentGenerator.g
@@ -26,6 +26,7 @@ options {
package com.facebook.swift.parser.antlr;
import com.facebook.swift.parser.model.*;
+ import com.facebook.swift.parser.util.*;
import java.util.ArrayList;
import java.util.HashMap;
@@ -133,8 +134,14 @@ const_map returns [Map<ConstValue, ConstValue> value = new HashMap<>()]
: ^(MAP ( ^(ENTRY k=const_value v=const_value) { $value.put($k.value, $v.value); } )*)
;
-enum_fields returns [List<IntegerEnumField> value = new ArrayList<>()]
- : ( ^(k=IDENTIFIER v=integer?) { $value.add(new IntegerEnumField($k.text, $v.value)); } )*
+enum_fields returns [IntegerEnumFieldList value = new IntegerEnumFieldList()]
+ : ( enum_field[$value] { $value.add($enum_field.value); } )*
+ ;
+
+enum_field[IntegerEnumFieldList fieldList] returns [IntegerEnumField value]
+ : ^(k=IDENTIFIER v=integer?) {
+ $value = new IntegerEnumField($k.text, $v.value, $fieldList.getNextImplicitEnumerationValue());
+ }
;
senum_values returns [List<String> value = new ArrayList<>()]
View
21 swift-idl-parser/src/main/java/com/facebook/swift/parser/model/IntegerEnumField.java
@@ -23,30 +23,39 @@
public class IntegerEnumField
{
private final String name;
- private final Optional<Long> value;
+ private final Optional<Long> explicitValue;
- public IntegerEnumField(String name, Long value)
+ private final long effectiveValue;
+
+ public IntegerEnumField(String name, Long explicitValue, Long defaultValue)
{
this.name = checkNotNull(name, "name");
- this.value = Optional.fromNullable(value);
+ this.explicitValue = Optional.fromNullable(explicitValue);
+ this.effectiveValue = (this.explicitValue.isPresent()) ? this.explicitValue.get() : defaultValue;
}
public String getName()
{
return name;
}
- public Optional<Long> getValue()
+ public Optional<Long> getExplicitValue()
+ {
+ return explicitValue;
+ }
+
+ public long getValue()
{
- return value;
+ return effectiveValue;
}
@Override
public String toString()
{
return Objects.toStringHelper(this)
.add("name", name)
- .add("value", value)
+ .add("value", effectiveValue)
+ .add("explicitValue", explicitValue)
.toString();
}
}
View
34 swift-idl-parser/src/main/java/com/facebook/swift/parser/util/IntegerEnumFieldList.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2012 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License. You may obtain
+ * a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package com.facebook.swift.parser.util;
+
+import com.facebook.swift.parser.model.IntegerEnumField;
+
+import java.util.ArrayList;
+
+public class IntegerEnumFieldList extends ArrayList<IntegerEnumField>
+{
+ public long getNextImplicitEnumerationValue()
+ {
+ int lastFieldIndex = size() - 1;
+ if (lastFieldIndex < 0) {
+ // No previous values to derive from, and thrift enumerations start at zero by default
+ return 0;
+ }
+ IntegerEnumField lastField = get(lastFieldIndex);
+ return lastField.getValue() + 1;
+ }
+}

0 comments on commit 0bbd0d9

Please sign in to comment.
Something went wrong with that request. Please try again.