Skip to content

Commit a1c99e8

Browse files
authored
Lenses reserved word fix (#539)
lenses generation now checks for reserved words
1 parent 02bacbc commit a1c99e8

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

cli/src/main/scala/scalaxb/compiler/xsd/GenLens.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package scalaxb.compiler.xsd
22

3-
import scalaxb.compiler.Config
3+
import scalaxb.compiler.{Config, ScalaNames}
44

55
trait GenLens { self: ContextProcessor =>
66
def buildImport: String
@@ -32,6 +32,8 @@ trait GenLens { self: ContextProcessor =>
3232
* @param config
3333
*/
3434
class GenMonocleLens(var config: Config) extends GenLens with ContextProcessor {
35+
lazy val scalaNames: ScalaNames = new ScalaNames {}
36+
def escapeKeyWord(name: String) = if(scalaNames.isKeyword(name)) s"`$name`" else name
3537

3638
def buildImport: String = {
3739
""
@@ -40,7 +42,7 @@ class GenMonocleLens(var config: Config) extends GenLens with ContextProcessor {
4042
def buildDefLens(className : String, param: Params#Param) : String = {
4143
s"def ${param.toParamName}: monocle.Lens[$className, ${param.typeName}] = " +
4244
s"monocle.Lens[$className, ${param.typeName}](_.${param.toParamName})" +
43-
s"((_${param.toParamName}: ${param.typeName}) => (${className.toLowerCase}: $className) => ${className.toLowerCase}.copy(${param.toParamName} = _${param.toParamName}))"
45+
s"((_${param.toParamName}: ${param.typeName}) => (${escapeKeyWord(className.toLowerCase)}: $className) => ${escapeKeyWord(className.toLowerCase)}.copy(${param.toParamName} = _${param.toParamName}))"
4446
}
4547

4648
def buildDefComposeLens(className : String, param: Params#Param) : String = {

integration/src/test/resources/ipo.xsd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,10 @@
318318
</extension>
319319
</simpleContent>
320320
</complexType>
321+
322+
<complexType name="Case">
323+
<sequence>
324+
<element type="string" name="Id"/>
325+
</sequence>
326+
</complexType>
321327
</schema>

0 commit comments

Comments
 (0)