代码拉取完成,页面将自动刷新
package template
import (
"gitee.com/h79/goutils/common/json"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/result"
"text/template"
)
type Format struct {
engine *Engine
command template.FuncMap
items Items
}
type Handler func(source interface{}, d D) error
type Command func(source interface{}, d D, handler Handler) error
func DefFormat() *Format {
engine := DefEngine()
return NewFormat(engine)
}
func NewFormat(engine *Engine) *Format {
return &Format{
engine: engine,
command: make(template.FuncMap),
}
}
func (ft *Format) Load(path string) error {
if err := json.Read(path, &ft.items); err != nil {
return err
}
logger.Debug("Template: conf= %+v", ft.items)
return nil
}
func (ft *Format) LoadTemplate(path string) {
ft.engine.LoadGlob(path)
}
func (ft *Format) AddCommand(funcName string, funcCall Command) {
ft.command[funcName] = funcCall
}
func (ft *Format) Format(format, appId, filter string, source interface{}, handler Handler) (*Data, error) {
reply, exists := ft.items.ItMap[appId]
if !exists {
return nil, result.Error(result.ErrNotFound, "Not found config")
}
var tmpl Content
if format == "xml" {
tmpl, exists = reply.Xml[filter]
} else if format == "json" {
tmpl, exists = reply.Json[filter]
} else {
return nil, result.Error(result.ErrException, "Not support the format")
}
if !exists {
return nil, result.Error(result.ErrNotFound, "Not found the filter content")
}
d := D{}
if f, ok := ft.command[tmpl.Func]; ok {
if err := f.(Command)(source, d, func(source interface{}, d D) error {
if handler != nil {
return handler(source, d)
}
return nil
}); err != nil {
}
}
return ft.engine.OUTPUT(tmpl.Name, d)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。