Skip to content
This repository

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

Open
wants to merge 2 commits into from

2 participants

Erik van oosten Jeff Smick
Erik van oosten

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

Jeff Smick
Collaborator

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.
2  scrooge-generator/src/main/scala/com/twitter/scrooge/backend/ConstsTemplate.scala
@@ -16,7 +16,7 @@ trait ConstsTemplate {
16 16 Dictionary(
17 17 "name" -> genID(c.sid),
18 18 "fieldType" -> genType(c.fieldType),
19   - "value" -> genConstant(c.value),
  19 + "value" -> genConstant(c.fieldType, c.value),
20 20 "docstring" -> codify(c.docstring.getOrElse(""))
21 21 )
22 22 })
20 scrooge-generator/src/main/scala/com/twitter/scrooge/backend/Generator.scala
@@ -131,28 +131,28 @@ abstract class Generator
131 131 case QualifiedID(names) => codify(names.map { quoteKeyword(_) }.mkString("."))
132 132 }
133 133
134   - def genConstant(constant: RHS, mutable: Boolean = false): CodeFragment = {
  134 + def genConstant(fieldType: FieldType, constant: RHS, mutable: Boolean = false): CodeFragment = {
135 135 constant match {
136 136 case NullLiteral => codify("null")
137 137 case StringLiteral(value) => codify(quote(value))
138 138 case DoubleLiteral(value) => codify(value.toString)
139 139 case IntLiteral(value) => codify(value.toString)
140 140 case BoolLiteral(value) => codify(value.toString)
141   - case c@ListRHS(_) => genList(c, mutable)
142   - case c@SetRHS(_) => genSet(c, mutable)
143   - case c@MapRHS(_) => genMap(c, mutable)
144   - case c: EnumRHS => genEnum(c)
  141 + case c@ListRHS(_) => genList(fieldType, c, mutable)
  142 + case c@SetRHS(_) => genSet(fieldType, c, mutable)
  143 + case c@MapRHS(_) => genMap(fieldType, c, mutable)
  144 + case c: EnumRHS => genEnum(fieldType, c)
145 145 case iv@IdRHS(id) => genID(id)
146 146 }
147 147 }
148 148
149   - def genList(list: ListRHS, mutable: Boolean = false): CodeFragment
  149 + def genList(fieldType: FieldType, list: ListRHS, mutable: Boolean = false): CodeFragment
150 150
151   - def genSet(set: SetRHS, mutable: Boolean = false): CodeFragment
  151 + def genSet(fieldType: FieldType, set: SetRHS, mutable: Boolean = false): CodeFragment
152 152
153   - def genMap(map: MapRHS, mutable: Boolean = false): CodeFragment
  153 + def genMap(fieldType: FieldType, map: MapRHS, mutable: Boolean = false): CodeFragment
154 154
155   - def genEnum(enum: EnumRHS): CodeFragment
  155 + def genEnum(fieldType: FieldType, enum: EnumRHS): CodeFragment
156 156
157 157 /**
158 158 * The default value for the specified type and mutability.
@@ -171,7 +171,7 @@ abstract class Generator
171 171 if (f.requiredness.isOptional) {
172 172 None
173 173 } else {
174   - f.default.map(genConstant(_, false)) orElse {
  174 + f.default.map(genConstant(f.fieldType, _, mutable = false)) orElse {
175 175 if (f.fieldType.isInstanceOf[ContainerType]) {
176 176 Some(genDefaultValue(f.fieldType))
177 177 } else {
21 scrooge-generator/src/main/scala/com/twitter/scrooge/backend/JavaGenerator.scala
@@ -84,28 +84,33 @@ class JavaGenerator(
84 84 }).asInstanceOf[N]
85 85 }
86 86
87   - def genList(list: ListRHS, mutable: Boolean = false): CodeFragment = {
  87 + def genList(fieldType: FieldType, list: ListRHS, mutable: Boolean = false): CodeFragment = {
  88 + val ListType(eltType, _) = fieldType
88 89 val code = (if (mutable) "Utilities.makeList(" else "Utilities.makeList(") +
89   - list.elems.map(genConstant(_).toData).mkString(", ") + ")"
  90 + list.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
90 91 codify(code)
91 92 }
92 93
93   - def genSet(set: SetRHS, mutable: Boolean = false): CodeFragment = {
  94 + def genSet(fieldType: FieldType, set: SetRHS, mutable: Boolean = false): CodeFragment = {
  95 + val SetType(eltType, _) = fieldType
94 96 val code = (if (mutable) "Utilities.makeSet(" else "Utilities.makeSet(") +
95   - set.elems.map(genConstant(_).toData).mkString(", ") + ")"
  97 + set.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
96 98 codify(code)
97 99 }
98 100
99   - def genMap(map: MapRHS, mutable: Boolean = false): CodeFragment = {
  101 + def genMap(fieldType: FieldType, map: MapRHS, mutable: Boolean = false): CodeFragment = {
  102 + val MapType(keyType, valueType, _) = fieldType
100 103 val code = (if (mutable) "Utilities.makeMap(" else "Utilities.makeMap(") + (map.elems.map {
101 104 case (k, v) =>
102   - "Utilities.makeTuple(" + genConstant(k).toData + ", " + genConstant(v).toData + ")"
  105 + "Utilities.makeTuple(" + genConstant(keyType, k).toData + ", " + genConstant(valueType, v).toData + ")"
103 106 } mkString (", ")) + ")"
104 107 codify(code)
105 108 }
106 109
107   - def genEnum(enum: EnumRHS): CodeFragment =
108   - genID(enum.value.sid.toUpperCase.addScope(enum.enum.sid.toTitleCase))
  110 + def genEnum(fieldType: FieldType, enum: EnumRHS): CodeFragment = {
  111 + val enumType = fieldType.asInstanceOf[EnumType]
  112 + genID(enum.value.sid.toUpperCase.addScope(qualifyNamedType(enumType).toTitleCase))
  113 + }
109 114
110 115 /**
111 116 * Generates a suffix to append to a field expression that will
21 scrooge-generator/src/main/scala/com/twitter/scrooge/backend/ScalaGenerator.scala
@@ -80,28 +80,33 @@ class ScalaGenerator(
80 80 }).asInstanceOf[N]
81 81 }
82 82
83   - def genList(list: ListRHS, mutable: Boolean = false): CodeFragment = {
  83 + def genList(fieldType: FieldType, list: ListRHS, mutable: Boolean = false): CodeFragment = {
  84 + val ListType(eltType, _) = fieldType
84 85 val code = (if (mutable) "mutable.Buffer(" else "Seq(") +
85   - list.elems.map(genConstant(_).toData).mkString(", ") + ")"
  86 + list.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
86 87 codify(code)
87 88 }
88 89
89   - def genSet(set: SetRHS, mutable: Boolean = false): CodeFragment = {
  90 + def genSet(fieldType: FieldType, set: SetRHS, mutable: Boolean = false): CodeFragment = {
  91 + val SetType(eltType, _) = fieldType
90 92 val code = (if (mutable) "mutable.Set(" else "Set(") +
91   - set.elems.map(genConstant(_).toData).mkString(", ") + ")"
  93 + set.elems.map(genConstant(eltType, _).toData).mkString(", ") + ")"
92 94 codify(code)
93 95 }
94 96
95   - def genMap(map: MapRHS, mutable: Boolean = false): CodeFragment = {
  97 + def genMap(fieldType: FieldType, map: MapRHS, mutable: Boolean = false): CodeFragment = {
  98 + val MapType(keyType, valueType, _) = fieldType
96 99 val code = (if (mutable) "mutable.Map(" else "Map(") + (map.elems.map {
97 100 case (k, v) =>
98   - genConstant(k).toData + " -> " + genConstant(v).toData
  101 + genConstant(keyType, k).toData + " -> " + genConstant(valueType, v).toData
99 102 } mkString (", ")) + ")"
100 103 codify(code)
101 104 }
102 105
103   - def genEnum(enum: EnumRHS): CodeFragment =
104   - genID(enum.value.sid.toTitleCase.addScope(enum.enum.sid.toTitleCase))
  106 + def genEnum(fieldType: FieldType, enum: EnumRHS): CodeFragment = {
  107 + val enumType = fieldType.asInstanceOf[EnumType]
  108 + genID(enum.value.sid.toTitleCase.addScope(qualifyNamedType(enumType).toTitleCase))
  109 + }
105 110
106 111 override def genDefaultValue(fieldType: FieldType, mutable: Boolean = false): CodeFragment = {
107 112 val code = fieldType match {
5 scrooge-generator/src/test/scala/com/twitter/scrooge/backend/ScalaGeneratorSpec.scala
@@ -627,6 +627,11 @@ class ScalaGeneratorSpec extends SpecificationWithJUnit with EvalHelper with JMo
627 627 candy.headline mustEqual "Life is short, eat dessert first"
628 628 }
629 629
  630 + "typedef relative fields with default" in {
  631 + val candy = Candy(100)
  632 + candy.candyType.value mustEqual 2
  633 + }
  634 +
630 635 "hide internal helper function to avoid naming conflict" in {
631 636 import thrift.`def`.default._
632 637 val impl = new NaughtyService.Iface {
2  scrooge-generator/src/test/thrift/relative/candy.thrift
@@ -18,7 +18,7 @@ typedef include1.CandyType CandyType
18 18
19 19 struct Candy {
20 20 1: i32 sweetness_iso
21   - 2: CandyType candy_type
  21 + 2: CandyType candy_type = CandyType.WEIRD
22 22 3: string headline = include1.HEADLINE
23 23 4: optional string brand = include2.BRAND
24 24 5: i32 count = include3.PIECES

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.