You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.2 KiB
78 lines
1.2 KiB
package render
|
|
|
|
import "gitoa.ru/go-4devs/mime"
|
|
|
|
type Option func(*Reference)
|
|
|
|
func WithParam(name string, val interface{}) Option {
|
|
return func(r *Reference) {
|
|
r.Params[name] = val
|
|
}
|
|
}
|
|
|
|
func WithEngine(name string) Option {
|
|
return func(r *Reference) {
|
|
r.Engine = name
|
|
}
|
|
}
|
|
|
|
func WithFormat(ext mime.Ext) Option {
|
|
return func(r *Reference) {
|
|
r.Format = ext
|
|
}
|
|
}
|
|
|
|
func NewReference(name string, opts ...Option) Reference {
|
|
ref := Reference{
|
|
name: name,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(&ref)
|
|
}
|
|
|
|
return ref
|
|
}
|
|
|
|
type Reference struct {
|
|
name string
|
|
Engine string
|
|
Format mime.Ext
|
|
Params Params
|
|
}
|
|
|
|
func (r Reference) Name() string {
|
|
if !r.Format.Is(mime.ExtUnrecognized) {
|
|
return r.name + "." + r.Format.String()
|
|
}
|
|
|
|
return r.name
|
|
}
|
|
|
|
func (r Reference) String() string {
|
|
if r.Engine != "" {
|
|
return r.name + "." + r.Format.String() + "." + r.Engine
|
|
}
|
|
|
|
return r.name + "." + r.Format.String()
|
|
}
|
|
|
|
func (r Reference) IsEngine(engines ...string) bool {
|
|
for _, engine := range engines {
|
|
if engine == r.Engine {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (r Reference) IsFromat(formats ...mime.Ext) bool {
|
|
return r.Format.Is(formats...)
|
|
}
|
|
|
|
type Params map[string]interface{}
|
|
|
|
func (p Params) Get(name string) interface{} {
|
|
return p[name]
|
|
}
|
|
|