-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from civitaspo/feature/param_eval
Feature/param eval
- Loading branch information
Showing
7 changed files
with
159 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
src/main/scala/pro/civitaspo/digdag/plugin/param/operator/AbstractParamOperator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
package pro.civitaspo.digdag.plugin.param.operator | ||
|
||
import io.digdag.client.config.Config | ||
import io.digdag.spi.OperatorContext | ||
import io.digdag.client.config.{Config, ConfigFactory} | ||
import io.digdag.spi.{OperatorContext, TemplateEngine} | ||
import io.digdag.util.BaseOperator | ||
import org.slf4j.{Logger, LoggerFactory} | ||
|
||
abstract class AbstractParamOperator(operatorName: String, context: OperatorContext) extends BaseOperator(context) { | ||
abstract class AbstractParamOperator(operatorName: String, context: OperatorContext, templateEngine: TemplateEngine) extends BaseOperator(context) { | ||
|
||
protected val logger: Logger = LoggerFactory.getLogger(operatorName) | ||
protected val cf: ConfigFactory = request.getConfig.getFactory | ||
protected val params: Config = request.getConfig | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/main/scala/pro/civitaspo/digdag/plugin/param/operator/ParamEvalOperator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package pro.civitaspo.digdag.plugin.param.operator | ||
import java.nio.charset.StandardCharsets.UTF_8 | ||
|
||
import com.google.common.collect.ImmutableList | ||
import io.digdag.client.config.{Config, ConfigKey} | ||
import io.digdag.spi.{OperatorContext, TaskResult, TemplateEngine} | ||
|
||
class ParamEvalOperator(operatorName: String, context: OperatorContext, templateEngine: TemplateEngine) | ||
extends AbstractParamOperator(operatorName, context, templateEngine) { | ||
|
||
protected val key: String = params.get("_command", classOf[String]) | ||
|
||
override def runTask(): TaskResult = { | ||
val evaluated: Config = eval(params) | ||
val paramsToStore: Config = cf.create() | ||
|
||
val elems: Seq[String] = key.split("\\.") | ||
val parents: Seq[String] = elems.reverse.tail.reverse | ||
val child: String = elems.last | ||
|
||
if (parents.isEmpty) paramsToStore.set(child, evaluated.get(child, classOf[Object])) | ||
else { | ||
val getter = parents.foldLeft(evaluated) { (nested: Config, k: String) => | ||
nested.getNested(k) | ||
} | ||
val setter = parents.foldLeft(paramsToStore) { (nested: Config, k: String) => | ||
nested.getNestedOrSetEmpty(k) | ||
} | ||
setter.set(child, getter.get(child, classOf[Object])) | ||
} | ||
|
||
val builder = TaskResult.defaultBuilder(cf) | ||
builder.resetStoreParams(ImmutableList.of(ConfigKey.parse(key))) | ||
builder.storeParams(paramsToStore) | ||
builder.build() | ||
} | ||
|
||
protected def eval(params: Config): Config = { | ||
val tmpFile: String = workspace.createTempFile("param_eval", ".json") | ||
|
||
val writer = workspace.newBufferedWriter(tmpFile, UTF_8) | ||
try writer.write(params.toString) | ||
finally writer.close() | ||
|
||
val content = workspace.templateFile(templateEngine, tmpFile, UTF_8, params) | ||
cf.fromJsonString(content) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
src/main/scala/pro/civitaspo/digdag/plugin/param/operator/ParamStoreOperator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters