Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Enum default values, solves #47 (replaces pull request 49) #50

Open
wants to merge 2 commits into from

2 participants

@erikvanoosten

solves #47: Support fields with an enum constant that is included from another thrift file.

@sprsquish

The patch wont apply due to a conflict in scrooge-generator/src/main/scala/com/twitter/scrooge/backend/ConstsTemplate.scala.
Can you rebase on to master please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  scrooge-generator/src/main/scala/com/twitter/scrooge/backend/ConstsTemplate.scala
@@ -16,7 +16,7 @@ trait ConstsTemplate {
Dictionary(
"name" -> genID(c.sid),
"fieldType" -> genType(c.fieldType),
- "value" -> genConstant(c.value),
+ "value" -> genConstant(c.fieldType, c.value),
"docstring" -> codify(c.docstring.getOrElse(""))
)
})
View
20 scrooge-generator/src/main/scala/com/twitter/scrooge/backend/Generator.scala
@@ -131,28 +131,28 @@ abstract class Generator
case QualifiedID(names) => codify(names.map { quoteKeyword(_) }.mkString("."))
}
- def genConstant(constant: RHS, mutable: Boolean = false): CodeFragment = {
+ def genConstant(fieldType: FieldType, constant: RHS, mutable: Boolean = false): CodeFragment = {
constant match {
case NullLiteral => codify("null")
case StringLiteral(value) => codify(quote(value))
case DoubleLiteral(value) => codify(value.toString)
case IntLiteral(value) => codify(value.toString)
case BoolLiteral(value) => codify(value.toString)
- 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@ListRHS(_) => genList(fieldType, c, mutable)
+ case c@SetRHS(_) => genSet(fieldType, c, mutable)
+ case c@MapRHS(_) => genMap(fieldType, c, mutable)
+ case c: EnumRHS => genEnum(fieldType, c)
case iv@IdRHS(id) => genID(id)
}
}
- def genList(list: ListRHS, mutable: Boolean = false): CodeFragment
+ def genList(fieldType: FieldType, list: ListRHS, mutable: Boolean = false): CodeFragment
- def genSet(set: SetRHS, mutable: Boolean = false): CodeFragment
+ def genSet(fieldType: FieldType, set: SetRHS, mutable: Boolean = false): CodeFragment
- def genMap(map: MapRHS, mutable: Boolean = false): CodeFragment
+ def genMap(fieldType: FieldType, map: MapRHS, mutable: Boolean = false): CodeFragment
- def genEnum(enum: EnumRHS): CodeFragment
+ def genEnum(fieldType: FieldType, enum: EnumRHS): CodeFragment
/**
* The default value for the specified type and mutability.
@@ -171,7 +171,7 @@ abstract class Generator
if (f.requiredness.isOptional) {
None
} else {
- f.default.map(genConstant(_, false)) orElse {
+ f.default.map(genConstant(f.fieldType, _, mutable = false)) orElse {
if (f.fieldType.isInstanceOf[ContainerType]) {
Some(genDefaultValue(f.fieldType))
} else {
View
21 scrooge-generator/src/main/scala/com/twitter/scrooge/backend/JavaGenerator.scala
@@ -84,28 +84,33 @@ class JavaGenerator(
}).asInstanceOf[N]
}
- def genList(list: ListRHS, mutable: Boolean = false): CodeFragment = {
+ def genList(fieldType: FieldType, list: ListRHS, mutable: Boolean = false): CodeFragment = {
+ val ListType(eltType, _) = fieldType
val code = (if (mutable) "Utilities.makeList(" else "Utilities.makeList(") +
- list.elems.map(genConstant(_).toData).mkString(", ") + ")"
+ list.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
codify(code)
}
- def genSet(set: SetRHS, mutable: Boolean = false): CodeFragment = {
+ def genSet(fieldType: FieldType, set: SetRHS, mutable: Boolean = false): CodeFragment = {
+ val SetType(eltType, _) = fieldType
val code = (if (mutable) "Utilities.makeSet(" else "Utilities.makeSet(") +
- set.elems.map(genConstant(_).toData).mkString(", ") + ")"
+ set.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
codify(code)
}
- def genMap(map: MapRHS, mutable: Boolean = false): CodeFragment = {
+ def genMap(fieldType: FieldType, map: MapRHS, mutable: Boolean = false): CodeFragment = {
+ val MapType(keyType, valueType, _) = fieldType
val code = (if (mutable) "Utilities.makeMap(" else "Utilities.makeMap(") + (map.elems.map {
case (k, v) =>
- "Utilities.makeTuple(" + genConstant(k).toData + ", " + genConstant(v).toData + ")"
+ "Utilities.makeTuple(" + genConstant(keyType, k).toData + ", " + genConstant(valueType, v).toData + ")"
} mkString (", ")) + ")"
codify(code)
}
- def genEnum(enum: EnumRHS): CodeFragment =
- genID(enum.value.sid.toUpperCase.addScope(enum.enum.sid.toTitleCase))
+ def genEnum(fieldType: FieldType, enum: EnumRHS): CodeFragment = {
+ val enumType = fieldType.asInstanceOf[EnumType]
+ genID(enum.value.sid.toUpperCase.addScope(qualifyNamedType(enumType).toTitleCase))
+ }
/**
* Generates a suffix to append to a field expression that will
View
21 scrooge-generator/src/main/scala/com/twitter/scrooge/backend/ScalaGenerator.scala
@@ -80,28 +80,33 @@ class ScalaGenerator(
}).asInstanceOf[N]
}
- def genList(list: ListRHS, mutable: Boolean = false): CodeFragment = {
+ def genList(fieldType: FieldType, list: ListRHS, mutable: Boolean = false): CodeFragment = {
+ val ListType(eltType, _) = fieldType
val code = (if (mutable) "mutable.Buffer(" else "Seq(") +
- list.elems.map(genConstant(_).toData).mkString(", ") + ")"
+ list.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
codify(code)
}
- def genSet(set: SetRHS, mutable: Boolean = false): CodeFragment = {
+ def genSet(fieldType: FieldType, set: SetRHS, mutable: Boolean = false): CodeFragment = {
+ val SetType(eltType, _) = fieldType
val code = (if (mutable) "mutable.Set(" else "Set(") +
- set.elems.map(genConstant(_).toData).mkString(", ") + ")"
+ set.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
codify(code)
}
- def genMap(map: MapRHS, mutable: Boolean = false): CodeFragment = {
+ def genMap(fieldType: FieldType, map: MapRHS, mutable: Boolean = false): CodeFragment = {
+ val MapType(keyType, valueType, _) = fieldType
val code = (if (mutable) "mutable.Map(" else "Map(") + (map.elems.map {
case (k, v) =>
- genConstant(k).toData + " -> " + genConstant(v).toData
+ genConstant(keyType, k).toData + " -> " + genConstant(valueType, v).toData
} mkString (", ")) + ")"
codify(code)
}
- def genEnum(enum: EnumRHS): CodeFragment =
- genID(enum.value.sid.toTitleCase.addScope(enum.enum.sid.toTitleCase))
+ def genEnum(fieldType: FieldType, enum: EnumRHS): CodeFragment = {
+ val enumType = fieldType.asInstanceOf[EnumType]
+ genID(enum.value.sid.toTitleCase.addScope(qualifyNamedType(enumType).toTitleCase))
+ }
override def genDefaultValue(fieldType: FieldType, mutable: Boolean = false): CodeFragment = {
val code = fieldType match {
View
5 scrooge-generator/src/test/scala/com/twitter/scrooge/backend/ScalaGeneratorSpec.scala
@@ -627,6 +627,11 @@ class ScalaGeneratorSpec extends SpecificationWithJUnit with EvalHelper with JMo
candy.headline mustEqual "Life is short, eat dessert first"
}
+ "typedef relative fields with default" in {
+ val candy = Candy(100)
+ candy.candyType.value mustEqual 2
+ }
+
"hide internal helper function to avoid naming conflict" in {
import thrift.`def`.default._
val impl = new NaughtyService.Iface {
View
2  scrooge-generator/src/test/thrift/relative/candy.thrift
@@ -18,7 +18,7 @@ typedef include1.CandyType CandyType
struct Candy {
1: i32 sweetness_iso
- 2: CandyType candy_type
+ 2: CandyType candy_type = CandyType.WEIRD
3: string headline = include1.HEADLINE
4: optional string brand = include2.BRAND
5: i32 count = include3.PIECES
Something went wrong with that request. Please try again.