采用kotlin编写,可运行在jvm平台
Write in kotlin, can use on jvm platform
- 变量替换(text variable)
- 全局变量处理(global variable and provider)
- 嵌套变量处理(nested variable replacement)
- 支持参数,可用于格式化时间日期等(Support)
- 基础格式 Base format:
{xxx}
- 变量类型 Types:
var
"substring"
- 转义 escape:
{{
->{
, only in substring\"
->"
- 函数调用 Function:
{f a "b"}
-> Likef(a,"b")
- 使用任何空白字符分隔 split by any black: (like ' ' or '\n')
- 链式调用 Pipe invoke:
{f a "b"|f2 c}
-> Likef2(f(a,"b"),c)
- 使用'|'分隔,前面内容将作为第一个参数传递 split by '|', result as the first parameter
- 命名约束 Name Rule: name of variable
[^{}|\s]+
; inside const ""
- 变量未找到,返回null variable notFound: null
- 普通变量,返回原始值
unwarp
- 动态变量,递归
unwarp
返回 - 下面用
a->b
表示解析对象a
的子变量b
a.b
,如果存在a.b
直接返回,否则尝试解析 a->ba.b.c
:依次查找a.b.c
a.b->c
a->b->c
- 先根据变量模式解析
- 如果变量不存在,保持原样
- 尝试解析
x->toString
,否则调用Java的ToString函数
Step 1. Add the JitPack repository to your build file
allprojects{
repositories{
maven{ url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies{
implementation 'com.github.way-zer:PlaceHoldLib:Tag'
}
import cf.wayzer.placehold.PlaceHoldApi.with
println("Hello {v}".with("v" to "World")) //OUT: Hello World
PlaceHoldApi.registerTypeBinder(Date::class) { DateResolver(it) }
println("{t}".with("t" to Date(0))) //OUT 01-01
println("{t:yyyy-MM-dd}".with("t" to Date(0))) //OUT 1970-01-0
val v = DynamicResolver.new {
it ?: "NO Params"
}
println("{v}".with("v" to v))//OUT: NO Params
println("{v:}".with("v" to v))//OUT:
println("{v:123 456}".with("v" to v))//OUT 123 456
data class Data(val a: Int, val b: String)
PlaceHoldApi.registerGlobalVar("o", object : DynamicVar<Data>() {
override fun resolveThis(context: PlaceHoldContext): Data? {
return context.getVar("_o") as? Data
}
init {
registerChild("a") { it?.a }
registerChild("b") { it?.b }
}
override fun getThisSting(context: PlaceHoldContext): String? {
return resolveThis(context)?.toString()
}
})
println("{o.a} {o.b}".with("_o" to Data(22, "ab")))//OUT: 22 ab
println("{o.} {o}".with("_o" to Data(22, "ab")))//OUT: {o.} Data(a=22, b=ab)