Skip to content

Commit

Permalink
Fixes #288. Fixes default value handling of long attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Nov 6, 2014
1 parent 76dbdee commit e8307b4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 21 deletions.
25 changes: 19 additions & 6 deletions cli/src/main/scala/scalaxb/compiler/xsd/Args.scala
Expand Up @@ -58,11 +58,22 @@ trait Args extends Params {
wrapForLongAll: Boolean = false, formatter: Option[String] = None): String = {
val stack = "scalaxb.ElemName(node) :: stack"
def fromU = buildFromXML(typeName, "_", stackItem, formatter)

def fromValue(x: String) = buildFromXML(typeName, "scala.xml.Text(" + quote(x) + ")", stackItem, formatter)

val retval = if (wrapForLongAll) {
// PrefixedAttribute only contains pre, so you need to pass in node to get the namespace.
if (selector.contains("@")) selector + ".headOption map { x => scalaxb.DataRecord(x, node, " +
buildFromXML(typeName, "x", stackItem, formatter) + ") }"
if (selector.contains("@"))
(defaultValue, fixedValue) match {
case (_, Some(x)) =>
"Some(scalaxb.DataRecord(None, None, " + fromValue(x) + "))"
case (Some(x), _) =>
selector + ".headOption map { x => scalaxb.DataRecord(x, node, " +
buildFromXML(typeName, "x", stackItem, formatter) + ") } orElse " +
"Some(scalaxb.DataRecord(None, None, " + fromValue(x) + "))"
case _ =>
selector + ".headOption map { x => scalaxb.DataRecord(x, node, " +
buildFromXML(typeName, "x", stackItem, formatter) + ") }"
}
else selector + ".headOption map { x => scalaxb.DataRecord(x, " +
buildFromXML(typeName, "x", stackItem, formatter) + ") }"
} else (cardinality, nillable) match {
Expand Down Expand Up @@ -102,12 +113,14 @@ trait Args extends Params {
def buildArgForAttribute(decl: AttributeLike, stackItem: Option[String], longAttribute: Boolean): String =
if (longAttribute) {
decl match {
case attr: AttributeDecl =>
case attr: AttributeDecl =>
val o = attr.copy(use = OptionalUse)
val arg = buildArg(o, buildSelector(o), stackItem, longAttribute)
if (longAttribute) arg + " map { " + quote(buildNodeName(o, false)) + " -> _ }"
if (longAttribute) {
arg + " map { " + quote(buildNodeName(o, false)) + " -> _ }"
}
else arg
case ref: AttributeRef => buildArgForAttribute(buildAttribute(ref), stackItem, longAttribute)
case ref: AttributeRef => buildArgForAttribute(buildAttribute(ref), stackItem, longAttribute)
case group: AttributeGroupDecl => buildAttributeGroupArg(group, longAttribute)
}
} else buildArg(decl)
Expand Down
12 changes: 2 additions & 10 deletions cli/src/main/scala/scalaxb/compiler/xsd/GenSource.scala
Expand Up @@ -900,16 +900,8 @@ object {localName} {{
"lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
wrapperName + ".get(" + quote(buildNodeName(attr, false)) + ") map { _.as[" + buildTypeName(attr.typeSymbol, true) + "] }"
case (attr: AttributeDecl, Single) =>
attr.defaultValue match {
case Some(dv) =>
val typeName = buildTypeName(attr.typeSymbol, true)
"lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
wrapperName + ".get(" + quote(buildNodeName(attr, false)) + ").map( _.as[" + typeName +
s"""] ).getOrElse(scalaxb.fromXML[$typeName](<x>$dv</x>))"""
case None =>
"lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
wrapperName + "(" + quote(buildNodeName(attr, false)) + ").as[" + buildTypeName(attr.typeSymbol, true) + "]"
}
"lazy val " + makeParamName(buildParam(attr).name, true) + " = " +
wrapperName + "(" + quote(buildNodeName(attr, false)) + ").as[" + buildTypeName(attr.typeSymbol, true) + "]"
}
}

Expand Down
28 changes: 26 additions & 2 deletions integration/src/test/resources/GeneralUsage.scala
Expand Up @@ -42,6 +42,7 @@ object GeneralUsage {
testAll
testLongAll
testLongAttribute
testAnyAttribute
testTopLevelMultipleSeq
testTopLevelOptionalSeq
testTopLevelMustipleSeqAny
Expand Down Expand Up @@ -156,9 +157,9 @@ JDREVGRw==</base64Binary>
val obj = fromXML[SingularSimpleTypeTest](subject)

def check(obj: Any) = obj match {
case SingularSimpleTypeTest(1, None, None, Some(Some(1)), Seq(2, 1), Seq(), None,
case x@SingularSimpleTypeTest(1, None, None, Some(Some(1)), Seq(2, 1), Seq(), None,
WHOLE, None, None, None, Seq(WHOLE, SKIM), Seq(),
attrs) =>
attrs) if x.attr3 == WHOLE =>
case _ => sys.error("match failed: " + obj.toString)
}
check(obj)
Expand Down Expand Up @@ -422,6 +423,29 @@ JDREVGRw==</base64Binary>
val document = toXML[LongAttributeTest](obj, "foo", defaultScope)
println(document)
}

def testAnyAttribute {
println("testAnyAttribute")
val subject = <foo xmlns="http://www.example.com/general"
xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
milk1="SKIM"
xmime:contentType="foo" />
val obj = fromXML[AnyAttributeTest](subject)
def check(obj: AnyAttributeTest): Unit = {
obj.attributes("@milk1") match {
case DataRecord(_, _, "SKIM") =>
case _ => sys.error("match failed: " + obj.attributes("@milk1").toString + " " + obj.toString)
}
obj.attributes("@{http://www.w3.org/2005/05/xmlmime}contentType") match {
case DataRecord(_, _, "foo") =>
case _ => sys.error("match failed: " + obj.attributes("@{http://www.w3.org/2005/05/xmlmime}contentType").toString + " " + obj.toString)
}
}
check(obj)
val document = toXML[AnyAttributeTest](obj, "foo", defaultScope)
println(document)
check(fromXML[AnyAttributeTest](document))
}

def testTopLevelMultipleSeq {
println("testTopLevelMultipleSeq")
Expand Down
9 changes: 8 additions & 1 deletion integration/src/test/resources/general.xsd
Expand Up @@ -147,7 +147,8 @@
</xs:sequence>

<xs:attribute name="attr1" type="xs:unsignedInt"/>
<xs:attribute name="attr2" type="gen:MilkType"/>
<xs:attribute name="attr2" type="gen:MilkType"/>
<xs:attribute name="attr3" type="gen:MilkType" default="WHOLE" />
</xs:complexType>

<xs:complexType name="ListTest">
Expand Down Expand Up @@ -573,6 +574,12 @@
</xs:complexType>
</xs:element>

<xs:element name="anyAttributeTest">
<xs:complexType>
<xs:anyAttribute namespace="##any"/>
</xs:complexType>
</xs:element>

<xs:complexType name="anySimpleTypeExtension">
<xs:simpleContent>
<xs:extension base="xs:anySimpleType">
Expand Down
1 change: 1 addition & 0 deletions integration/src/test/resources/xmlmime.xsd
Expand Up @@ -50,5 +50,6 @@
<xs:sequence minOccurs="0">
<xs:element name="string1" type="xs:string" />
</xs:sequence>
<xs:attribute name="attr1" type="xs:anyURI" default="urn:foo" />
</xs:complexType>
</xs:schema>
6 changes: 4 additions & 2 deletions notes/1.3.0.markdown
Expand Up @@ -24,10 +24,12 @@

### case class >22 and attributes change

Starting scalaxb 1.3.0, the generated code will be >22 by default. In addition, all attributes will be handled via `attributes` field.
Starting scalaxb 1.3.0, the generated code will be >22 by default. In addition, all attributes will be handled via `attributes` field.

To bring back the older behavior
To bring back the older behavior:

contentsSizeLimit in (Compile, scalaxb) := 20

namedAttributes in (Compile, scalaxb) := true

Related, 1.3.0 fixes attribute's default value handling [#288][288].

0 comments on commit e8307b4

Please sign in to comment.