3 Star 0 Fork 0

Gitee 极速下载/Configurator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/progrium/configurator
克隆/下载
preprocessor.go 1009 Bytes
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"path/filepath"
"sync"
)
type macroinput map[string]interface{}
type macrofn func(macroinput) interface{}
type Preprocessor struct {
sync.Mutex
macros map[string]macrofn
ready bool
}
func (p *Preprocessor) Register(name string, fn macrofn) {
p.Lock()
defer p.Unlock()
if !p.ready {
p.macros = make(map[string]macrofn)
p.ready = true
}
p.macros[name] = fn
}
func (p *Preprocessor) Process(tree *JsonTree) *JsonTree {
p.Lock()
defer p.Unlock()
newtree := tree.Copy()
for _, namepath := range p.macroPaths(newtree) {
name := filepath.Base(namepath)
path := filepath.Dir(namepath)
input := macroinput(tree.Get(path).(map[string]interface{}))
newtree.Replace(path, p.macros[name](input))
}
return newtree
}
func (p *Preprocessor) macroPaths(t *JsonTree) []string {
paths := make([]string, 0)
for _, path := range t.Paths() {
for name, _ := range p.macros {
if filepath.Base(path) == name {
paths = append(paths, path)
}
}
}
return paths
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/Configurator.git
git@gitee.com:mirrors/Configurator.git
mirrors
Configurator
Configurator
master

搜索帮助