Skip to content

Commit

Permalink
Switched from kotlin-mustache to mustache-k
Browse files Browse the repository at this point in the history
  • Loading branch information
pwall567 committed Sep 5, 2024
1 parent 8c6e5ae commit 4c7233e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
### Changed
- `CodeGenerator`, `Configurator`, `Constraints`, `Target`: Switched from `jsonutil` library to `kjson-core`, and from
`yaml-simple` to `kjson-yaml`
- `CodeGenerator`, `Configurer`, `Target`, `Annotated`: Switched from `kotlin-mustache` to `mustache-k`
- `CodeGenerator`: added explicit types to public APIs

## [0.109] - 2024-09-05
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Code generation for JSON Schema (Draft 07).
[`kjson-core`](https://github.com/pwall567/kjson-core) and [`kjson-yaml`](https://github.com/pwall567/kjson-yaml).
The change should be transparent to most users.

Also, the Mustache processor has been switched from `kotlin-mustache` to `mustache-k`, but this change will almost
certainly not affect any users.

New in version 0.106 – the `classNames` configuration option has been extended to allow the configuration of
generated nested class names.
See [`classNames`](CONFIG.md#classnames) in the [Configuration Guide](CONFIG.md) for more details.
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
<version>2.4</version>
</dependency>
<dependency>
<groupId>net.pwall.mustache</groupId>
<artifactId>kotlin-mustache</artifactId>
<version>0.12</version>
<groupId>io.kjson</groupId>
<artifactId>mustache-k</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>io.kjson</groupId>
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/net/pwall/json/schema/codegen/Annotated.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

package net.pwall.json.schema.codegen

import net.pwall.mustache.Context
import net.pwall.mustache.Template
import io.kjson.mustache.Context
import io.kjson.mustache.Template

/**
* Base class for output constructs that can have Java/Kotlin annotations.
Expand Down
47 changes: 17 additions & 30 deletions src/main/kotlin/net/pwall/json/schema/codegen/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf

import java.io.File
import java.io.Reader
import java.math.BigDecimal
import java.net.URI
import java.nio.file.Files
Expand All @@ -48,9 +47,13 @@ import io.kjson.JSONNumber
import io.kjson.JSONObject
import io.kjson.JSONString
import io.kjson.JSONValue
import io.kjson.mustache.Template
import io.kjson.mustache.parser.Parser as MustacheParser
import io.kjson.mustache.Context
import io.kjson.pointer.JSONPointer
import io.kjson.pointer.JSONRef
import io.kjson.pointer.get
import io.kjson.resource.Resource
import io.kjson.yaml.YAML

import net.pwall.json.schema.JSONSchema
Expand Down Expand Up @@ -81,9 +84,6 @@ import net.pwall.json.schema.validation.TypeValidator
import net.pwall.json.schema.validation.UniqueItemsValidator
import net.pwall.log.Log.getLogger
import net.pwall.log.Logger
import net.pwall.mustache.Context
import net.pwall.mustache.Template
import net.pwall.mustache.parser.Parser as MustacheParser
import net.pwall.util.DefaultValue
import net.pwall.util.Strings

Expand Down Expand Up @@ -162,38 +162,29 @@ class CodeGenerator(

@Suppress("MemberVisibilityCanBePrivate")
var templateParser: MustacheParser by DefaultValue {
MustacheParser { name ->
partialResolver(name)
MustacheParser().apply {
for (directory in targetLanguage.directories) {
val url = Resource.classPathURL("/$directory/") ?: fatal("Can't locate template directory /$directory")
addDirectory(url)
}
}
}

private fun partialResolver(name: String): Reader {
for (dir in targetLanguage.directories)
CodeGenerator::class.java.getResourceAsStream("/$dir/$name.mustache")?.let { return it.reader() }
fatal("Can't locate template partial $name")
}

var template: Template by DefaultValue {
val parser = templateParser
val resolver = parser.resolvePartial
parser.parse(parser.resolver(templateName))
templateParser.parseByName(templateName)
}

@Suppress("MemberVisibilityCanBePrivate")
var interfaceTemplateName = "interface"

@Suppress("MemberVisibilityCanBePrivate")
var interfaceTemplate: Template by DefaultValue {
val parser = templateParser
val resolver = parser.resolvePartial
parser.parse(parser.resolver(interfaceTemplateName))
templateParser.parseByName(interfaceTemplateName)
}

@Suppress("MemberVisibilityCanBePrivate")
var enumTemplate: Template by DefaultValue {
val parser = templateParser
val resolver = parser.resolvePartial
parser.parse(parser.resolver(enumTemplateName))
templateParser.parseByName(enumTemplateName)
}

var indexFileName: TargetFileName? = null
Expand All @@ -203,9 +194,7 @@ class CodeGenerator(

@Suppress("MemberVisibilityCanBePrivate")
var indexTemplate: Template by DefaultValue {
val parser = templateParser
val resolver = parser.resolvePartial
parser.parse(parser.resolver(indexTemplateName))
templateParser.parseByName(indexTemplateName)
}

var outputResolver: OutputResolver by DefaultValue {
Expand Down Expand Up @@ -248,16 +237,14 @@ class CodeGenerator(

})

fun setTemplateDirectory(directory: File, suffix: String = "mustache") {
fun setTemplateDirectory(directory: File) {
when {
directory.isFile -> fatal("Template directory must be a directory")
directory.isDirectory -> {}
else -> fatal("Error accessing template directory")
}
templateParser = MustacheParser().also {
it.resolvePartial = { name ->
File(directory, "$name.$suffix").reader()
}
templateParser = MustacheParser().apply {
addDirectory(directory)
}
}

Expand Down Expand Up @@ -623,7 +610,7 @@ class CodeGenerator(
indexFileName?.let { name ->
log.info { "-- index $name" }
outputResolver(name).use {
indexTemplate.processTo(AppendableFilter(it), TargetIndex(
indexTemplate.renderTo(AppendableFilter(it), TargetIndex(
targets = targets.filter { t ->
t.constraints.isObject ||
t.constraints.isEnumClass && t.constraints.enumValues.let { e ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import io.kjson.JSONBoolean
import io.kjson.JSONObject
import io.kjson.JSONString
import io.kjson.JSONValue
import io.kjson.mustache.Template
import io.kjson.pointer.JSONPointer
import io.kjson.pointer.JSONRef
import io.kjson.pointer.forEachKey
Expand All @@ -44,7 +45,6 @@ import net.pwall.json.schema.JSONSchema
import net.pwall.json.schema.codegen.CodeGenerator.Companion.fatal
import net.pwall.json.schema.output.BasicErrorEntry
import net.pwall.json.schema.validation.FormatValidator
import net.pwall.mustache.Template

object Configurator {

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/net/pwall/json/schema/codegen/Target.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
package net.pwall.json.schema.codegen

import io.kjson.JSONValue
import io.kjson.mustache.Context
import io.kjson.mustache.Template

import net.pwall.json.schema.JSONSchema
import net.pwall.json.schema.JSONSchemaException
import net.pwall.json.schema.codegen.CodeGenerator.Companion.addOnce
import net.pwall.json.schema.subschema.RefSchema
import net.pwall.mustache.Context
import net.pwall.mustache.Template

/**
* A code generation target. The class contains several properties that exist just for the purposes of template
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ import kotlin.test.expect
import java.io.File

import io.kjson.JSON
import io.kjson.mustache.Template
import io.kjson.pointer.JSONPointer

import net.pwall.json.schema.codegen.CodeGeneratorTestUtil.OutputDetails
import net.pwall.json.schema.codegen.CodeGeneratorTestUtil.createHeader
import net.pwall.json.schema.codegen.CodeGeneratorTestUtil.dirs
import net.pwall.json.schema.codegen.CodeGeneratorTestUtil.outputCapture
import net.pwall.mustache.Template

class CodeGeneratorMultipleTest {

Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/net/pwall/json/schema/codegen/IndentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ import kotlin.test.expect

import java.io.StringReader

import net.pwall.mustache.Template
import io.kjson.mustache.Template

class IndentTest {

@Test fun `should output no indent initially`() {
val template = Template.parse(StringReader("[{{&indent}}]"))
expect("[]") { template.processToString(Indent()) }
expect("[]") { template.render(Indent()) }
}

@Test fun `should output incremented indent`() {
val template = Template.parse(StringReader("[{{#indent.increment}}{{&indent}}{{/indent.increment}}]"))
expect("[ ]") { template.processToString(Indent()) }
expect("[ ]") { template.render(Indent()) }
}

@Test fun `should output doubly incremented indent`() {
val template = Template.parse(StringReader(
"[{{#indent.increment}}{{#indent.increment}}{{&indent}}{{/indent.increment}}{{/indent.increment}}]"))
expect("[ ]") { template.processToString(Indent()) }
expect("[ ]") { template.render(Indent()) }
}

}

0 comments on commit 4c7233e

Please sign in to comment.