Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[split] scrooge: properly handle field annotations

RB_ID=265125
  • Loading branch information...
commit eb0833ee72efc38104fede5255e3fd61ec83a16d 1 parent 1846e5b
@sprsquish sprsquish authored CI committed
View
4 scrooge-generator/src/main/scala/com/twitter/scrooge/AST/Node.scala
@@ -41,8 +41,8 @@ case class Field(
fieldType: FieldType,
default: Option[RHS] = None,
requiredness: Requiredness = Requiredness.Default,
- fieldAnnotations: Map[String, String] = Map.empty,
- defaultValueAnnotations: Map[String, String] = Map.empty
+ typeAnnotations: Map[String, String] = Map.empty,
+ fieldAnnotations: Map[String, String] = Map.empty
) extends Node
case class Function(
View
45 scrooge-generator/src/main/scala/com/twitter/scrooge/frontend/ThriftParser.scala
@@ -189,32 +189,29 @@ class ThriftParser(
lazy val field = (opt(comments) ~> opt(fieldId) ~ fieldReq) ~
(fieldType ~ defaultedAnnotations ~ simpleID) ~
- opt("=" ~> (rhs ~ defaultedAnnotations)) <~ opt(listSeparator) ^^ {
- case (fid ~ req) ~ (ftype ~ fieldAnnotations ~ sid) ~ optDefault => {
- val value = optDefault map { _._1 }
- val valueAnnotations = optDefault map { _._2 } getOrElse Map.empty
-
- val transformedVal = ftype match {
- case TBool => value map {
- case IntLiteral(0) => BoolLiteral(false)
- case _ => BoolLiteral(true)
+ opt("=" ~> rhs) ~ defaultedAnnotations <~ opt(listSeparator) ^^ {
+ case (fid ~ req) ~ (ftype ~ typeAnnotations ~ sid) ~ value ~ fieldAnnotations => {
+ val transformedVal = ftype match {
+ case TBool => value map {
+ case IntLiteral(0) => BoolLiteral(false)
+ case _ => BoolLiteral(true)
+ }
+ case _ => value
}
- case _ => value
- }
- // if field is marked optional and a default is defined, ignore the optional part.
- val transformedReq = if (!defaultOptional && transformedVal.isDefined && req.isOptional) Requiredness.Default else req
-
- Field(
- fid.getOrElse(0),
- sid,
- sid.name,
- ftype,
- transformedVal,
- transformedReq,
- fieldAnnotations,
- valueAnnotations
- )
+ // if field is marked optional and a default is defined, ignore the optional part.
+ val transformedReq = if (!defaultOptional && transformedVal.isDefined && req.isOptional) Requiredness.Default else req
+
+ Field(
+ fid.getOrElse(0),
+ sid,
+ sid.name,
+ ftype,
+ transformedVal,
+ transformedReq,
+ typeAnnotations,
+ fieldAnnotations
+ )
}
}
View
15 scrooge-generator/src/test/scala/com/twitter/scrooge/frontend/ThriftParserSpec.scala
@@ -332,9 +332,10 @@ enum Foo
Map("dbtype" -> "fixedchar(4)", "nullable" -> "false")
))
- val idAnnotations = Map("autoincrement" -> "true")
- val idValueAnnotations = Map("initialValue" -> "0")
- val codeAnnotations = Map("dbtype" -> "varchar(255)")
+ val idTypeAnnotations = Map("autoincrement" -> "true")
+ val idFieldAnnotations = Map("initialValue" -> "0")
+ val codeTypeAnnotations = Map("dbtype" -> "varchar(255)")
+ val nameFieldAnnotations = Map("postid" -> "varchar(255)")
val structAnnotations = Map(
"primary_key" -> "(id)",
"index" -> "code_idx(code)",
@@ -345,7 +346,7 @@ enum Foo
struct Airport {
1: optional i64 (autoincrement="true") id = 0(initialValue="0"),
2: optional string(dbtype="varchar(255)") code,
- 3: optional string name
+ 3: optional string name(postid="varchar(255)")
} (primary_key="(id)",
index="code_idx(code)",
sql_name="airports",)
@@ -355,9 +356,9 @@ enum Foo
SimpleID("Airport"),
"Airport",
Seq(
- Field(1, SimpleID("id"), "id", TI64, Some(IntLiteral(0)), Requiredness.Default, idAnnotations, idValueAnnotations),
- Field(2, SimpleID("code"), "code", TString, None, Requiredness.Optional, codeAnnotations, Map.empty),
- Field(3, SimpleID("name"), "name", TString, None, Requiredness.Optional, Map.empty, Map.empty)
+ Field(1, SimpleID("id"), "id", TI64, Some(IntLiteral(0)), Requiredness.Default, idTypeAnnotations, idFieldAnnotations),
+ Field(2, SimpleID("code"), "code", TString, None, Requiredness.Optional, codeTypeAnnotations, Map.empty),
+ Field(3, SimpleID("name"), "name", TString, None, Requiredness.Optional, Map.empty, nameFieldAnnotations)
),
None,
structAnnotations
Please sign in to comment.
Something went wrong with that request. Please try again.