Skip to content

Commit

Permalink
Merge pull request #610 from xuwei-k/quote-enum
Browse files Browse the repository at this point in the history
avoid `enum` name
  • Loading branch information
eed3si9n authored Oct 5, 2023
2 parents 346e4e7 + 3f76473 commit 0440b7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions cli/src/main/scala/scalaxb/compiler/xsd/ContextProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ trait ContextProcessor extends ScalaNames with PackageName {
def makeEnumValues(decl: SimpleTypeDecl, scope: scala.xml.NamespaceBinding, context: XsdContext) : Unit = {
val enumValues = context.enumValueNames(packageName(decl.namespace, context))
val name = context.typeNames(decl)
filterEnumeration(decl) map { enum =>
enumValues(name -> enum) = makeProtectedTypeName(decl.namespace,
enum.value match {
filterEnumeration(decl) map { enumDecl =>
enumValues(name -> enumDecl) = makeProtectedTypeName(decl.namespace,
enumDecl.value match {
case qname: QName => Option(scope.getPrefix(qname.getNamespaceURI)).getOrElse("") + qname.getLocalPart.capitalize
case x if enum.value.toString.length > enumNameMaxLength => "longName"
case _ => enum.value.toString
case x if enumDecl.value.toString.length > enumNameMaxLength => "longName"
case _ => enumDecl.value.toString
}, "Value", context)
}
}
Expand Down
16 changes: 8 additions & 8 deletions cli/src/main/scala/scalaxb/compiler/xsd/GenSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -616,22 +616,22 @@ class GenSource(val schema: SchemaDecl,
val baseSym : Option[XsTypeSymbol] = decl.content match {case SimpTypRestrictionDecl(base, _) => Some(base) case _ => None}
val baseType: Option[String ] = baseSym.map(buildTypeName(_))

def makeEnum(enum: EnumerationDecl[_]) =
"case object " + buildTypeName(localName, enum, true) + " extends " + localName +
" { override def toString = " + quote(enum.value.toString) + " }"
def makeEnum(enumDecl: EnumerationDecl[_]) =
"case object " + buildTypeName(localName, enumDecl, true) + " extends " + localName +
" { override def toString = " + quote(enumDecl.value.toString) + " }"

def makeCaseEntry(enum: EnumerationDecl[_]) = baseSym match {
case Some(XsQName) => s"${indent(3)}case ${quote(enum.value.toString)} => ${buildTypeName(localName, enum, false)}\n"
def makeCaseEntry(enumDecl: EnumerationDecl[_]) = baseSym match {
case Some(XsQName) => s"${indent(3)}case ${quote(enumDecl.value.toString)} => ${buildTypeName(localName, enumDecl, false)}\n"
case _ => baseType.map {tpe =>
s"${indent(3)}case x: $tpe if x == scalaxb.fromXML[$tpe](scala.xml.Text(${quote(enum.value.toString)})) => ${buildTypeName(localName, enum, false)}\n"
s"${indent(3)}case x: $tpe if x == scalaxb.fromXML[$tpe](scala.xml.Text(${quote(enumDecl.value.toString)})) => ${buildTypeName(localName, enumDecl, false)}\n"
}.getOrElse {
s"${indent(3)}case ${quote(enum.value.toString)} => ${buildTypeName(localName, enum, false)}\n"
s"${indent(3)}case ${quote(enumDecl.value.toString)} => ${buildTypeName(localName, enumDecl, false)}\n"
}
}


val enumString = enums.map(makeEnum).mkString(newline)
val enumListString = enums.map(enum => buildTypeName(localName, enum, true)).mkString(", ")
val enumListString = enums.map(enumDecl => buildTypeName(localName, enumDecl, true)).mkString(", ")
val enumValuesString = s"lazy val values: Seq[$localName] = Seq($enumListString)"

def valueCode: String = baseSym match {
Expand Down
10 changes: 5 additions & 5 deletions cli/src/main/scala/scalaxb/compiler/xsd/Lookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ trait Lookup extends ContextProcessor {
def buildTypeName(group: AttributeGroupDecl, shortLocal: Boolean): String =
buildTypeName(packageName(group, context), group, shortLocal)

def buildTypeName(enumTypeName: String, enum: EnumerationDecl[_], shortLocal: Boolean): String = {
def buildTypeName(enumTypeName: String, enumDecl: EnumerationDecl[_], shortLocal: Boolean): String = {
val pkg = packageName(schema, context)
val typeNames = context.enumValueNames(pkg)
if (!typeNames.contains(enumTypeName, enum))
sys.error(s"${pkg}: Type name not found: " + enum.toString)
if (!typeNames.contains(enumTypeName, enumDecl))
sys.error(s"${pkg}: Type name not found: " + enumDecl.toString)

if (shortLocal && pkg == packageName(schema, context)) typeNames(enumTypeName, enum)
else buildFullyQualifiedNameFromPackage(pkg, typeNames(enumTypeName, enum))
if (shortLocal && pkg == packageName(schema, context)) typeNames(enumTypeName, enumDecl)
else buildFullyQualifiedNameFromPackage(pkg, typeNames(enumTypeName, enumDecl))
}

def buildFullyQualifiedNameFromNS(namespace: Option[String], localName: String): String = {
Expand Down

0 comments on commit 0440b7e

Please sign in to comment.