Permalink
Browse files

[split] fixed issue when default value is enum from other namespace w…

…here namespace is missing in generated code

Signed-off-by: Jeff Smick <jeff@twitter.com>

RB_ID=282547
  • Loading branch information...
fmt-Println-MKO authored and CI committed Feb 6, 2014
1 parent 6515e0c commit 3d5c75fd2b298b9d0cbfb73413ef57ac46f7737e
@@ -169,7 +169,7 @@ trait Generator
case QualifiedID(names) => codify(names.map { quoteKeyword(_) }.mkString("."))
}
- def genConstant(constant: RHS, mutable: Boolean = false): CodeFragment = {
+ def genConstant(constant: RHS, mutable: Boolean = false, fieldType: Option[FieldType] = None): CodeFragment = {
constant match {
case NullLiteral => codify("null")
case StringLiteral(value) => codify(quote(value))
@@ -179,7 +179,7 @@ trait Generator
case c@ListRHS(_) => genList(c, mutable)
case c@SetRHS(_) => genSet(c, mutable)
case c@MapRHS(_) => genMap(c, mutable)
- case c: EnumRHS => genEnum(c)
+ case c: EnumRHS => genEnum(c, fieldType)
case iv@IdRHS(id) => genID(id)
}
}
@@ -190,7 +190,7 @@ trait Generator
def genMap(map: MapRHS, mutable: Boolean = false): CodeFragment
- def genEnum(enum: EnumRHS): CodeFragment
+ def genEnum(enum: EnumRHS, fieldType: Option[FieldType] = None): CodeFragment
/**
* The default value for the specified type and mutability.
@@ -209,7 +209,7 @@ trait Generator
if (f.requiredness.isOptional) {
None
} else {
- f.default.map(genConstant(_, false)) orElse {
+ f.default.map(genConstant(_, false, Some(f.fieldType))) orElse {
if (f.fieldType.isInstanceOf[ContainerType]) {
Some(genDefaultValue(f.fieldType))
} else {
@@ -116,8 +116,13 @@ class JavaGenerator(
codify(code)
}
- def genEnum(enum: EnumRHS): CodeFragment =
- genID(enum.value.sid.toUpperCase.addScope(enum.enum.sid.toTitleCase))
+ def genEnum(enum: EnumRHS, fieldType: Option[FieldType] = None): CodeFragment = {
+ def getTypeId: Identifier = fieldType.getOrElse(Void) match {
+ case n: NamedType => qualifyNamedType(n)
+ case _ => enum.enum.sid
+ }
+ genID(enum.value.sid.toUpperCase.addScope(getTypeId.toTitleCase))
+ }
/**
* Generates a suffix to append to a field expression that will
@@ -133,8 +133,13 @@ class ScalaGenerator(
codify(code)
}
- def genEnum(enum: EnumRHS): CodeFragment =
- genID(enum.value.sid.toTitleCase.addScope(enum.enum.sid.toTitleCase))
+ def genEnum(enum: EnumRHS, fieldType: Option[FieldType] = None): CodeFragment = {
+ def getTypeId: Identifier = fieldType.getOrElse(Void) match {
+ case n: NamedType => qualifyNamedType(n)
+ case _ => enum.enum.sid
+ }
+ genID(enum.value.sid.toTitleCase.addScope(getTypeId.toTitleCase))
+ }
override def genDefaultValue(fieldType: FieldType, mutable: Boolean = false): CodeFragment = {
val code = fieldType match {
@@ -843,6 +843,7 @@ class ScalaGeneratorSpec extends JMockSpec with EvalHelper {
candy.brand must be("Hershey")
candy.count must be(10)
candy.headline must be("Life is short, eat dessert first")
+ candy.defaultCandyType.value must be(0)
}
"hide internal helper function to avoid naming conflict" in { _ =>
@@ -22,4 +22,5 @@ struct Candy {
3: string headline = include1.HEADLINE
4: optional string brand = include2.BRAND
5: i32 count = include3.PIECES
+ 6: CandyType default_candy_type = CandyType.OLD
}

0 comments on commit 3d5c75f

Please sign in to comment.