2020-04-07 17:27:04 +03:00
|
|
|
package v1
|
2020-04-07 16:34:40 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TemplateTypeText is a text template type. There is no other types for now.
|
|
|
|
const TemplateTypeText = "text"
|
|
|
|
|
|
|
|
const (
|
2021-11-22 16:08:53 +03:00
|
|
|
// TemplateItemTypeText is a type for text chunk in template.
|
2020-04-08 13:50:02 +03:00
|
|
|
TemplateItemTypeText uint8 = iota
|
2021-11-22 16:08:53 +03:00
|
|
|
// TemplateItemTypeVar is a type for variable in template.
|
2020-04-07 16:34:40 +03:00
|
|
|
TemplateItemTypeVar
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-22 16:08:53 +03:00
|
|
|
// TemplateVarCustom is a custom variable type.
|
2020-04-07 16:34:40 +03:00
|
|
|
TemplateVarCustom = "custom"
|
2021-11-22 16:08:53 +03:00
|
|
|
// TemplateVarName is a name variable type.
|
2020-04-07 16:34:40 +03:00
|
|
|
TemplateVarName = "name"
|
2021-11-22 16:08:53 +03:00
|
|
|
// TemplateVarFirstName is a first name variable type.
|
2020-04-07 16:34:40 +03:00
|
|
|
TemplateVarFirstName = "first_name"
|
2021-11-22 16:08:53 +03:00
|
|
|
// TemplateVarLastName is a last name variable type.
|
2020-04-07 16:34:40 +03:00
|
|
|
TemplateVarLastName = "last_name"
|
|
|
|
)
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// templateVarAssoc for checking variable validity, only for internal use.
|
2020-04-07 16:34:40 +03:00
|
|
|
var templateVarAssoc = map[string]interface{}{
|
|
|
|
TemplateVarCustom: nil,
|
|
|
|
TemplateVarName: nil,
|
|
|
|
TemplateVarFirstName: nil,
|
|
|
|
TemplateVarLastName: nil,
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// Template struct.
|
2020-04-07 16:34:40 +03:00
|
|
|
type Template struct {
|
|
|
|
Code string `json:"code"`
|
2020-04-08 13:11:46 +03:00
|
|
|
ChannelID uint64 `json:"channel_id,omitempty"`
|
2020-04-07 16:34:40 +03:00
|
|
|
Name string `json:"name"`
|
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Template []TemplateItem `json:"template"`
|
2023-08-02 14:45:19 +03:00
|
|
|
Lang string `json:"lang,omitempty"`
|
|
|
|
Category string `json:"category,omitempty"`
|
2020-04-07 16:34:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// TemplateItem is a part of template.
|
2020-04-07 16:34:40 +03:00
|
|
|
type TemplateItem struct {
|
|
|
|
Type uint8
|
|
|
|
Text string
|
|
|
|
VarType string
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// MarshalJSON controls how TemplateItem will be marshaled into JSON.
|
2020-04-08 13:50:02 +03:00
|
|
|
func (t TemplateItem) MarshalJSON() ([]byte, error) {
|
2020-04-07 16:34:40 +03:00
|
|
|
switch t.Type {
|
|
|
|
case TemplateItemTypeText:
|
|
|
|
return json.Marshal(t.Text)
|
|
|
|
case TemplateItemTypeVar:
|
2020-04-08 13:50:02 +03:00
|
|
|
// {} case, fast output without marshaling
|
|
|
|
if t.VarType == "" || t.VarType == TemplateVarCustom {
|
|
|
|
return []byte("{}"), nil
|
|
|
|
}
|
|
|
|
|
2020-04-07 16:34:40 +03:00
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"var": t.VarType,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("unknown TemplateItem type")
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:08:53 +03:00
|
|
|
// UnmarshalJSON will correctly unmarshal TemplateItem.
|
2020-04-07 16:34:40 +03:00
|
|
|
func (t *TemplateItem) UnmarshalJSON(b []byte) error {
|
|
|
|
var obj interface{}
|
|
|
|
err := json.Unmarshal(b, &obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := obj.(type) {
|
|
|
|
case string:
|
|
|
|
t.Type = TemplateItemTypeText
|
|
|
|
t.Text = v
|
|
|
|
case map[string]interface{}:
|
|
|
|
// {} case
|
|
|
|
if len(v) == 0 {
|
|
|
|
t.Type = TemplateItemTypeVar
|
|
|
|
t.VarType = TemplateVarCustom
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if varTypeCurr, ok := v["var"].(string); ok {
|
2020-04-08 13:50:02 +03:00
|
|
|
if _, ok := templateVarAssoc[varTypeCurr]; !ok {
|
2020-04-07 16:34:40 +03:00
|
|
|
return fmt.Errorf("invalid placeholder var '%s'", varTypeCurr)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Type = TemplateItemTypeVar
|
2020-04-08 13:50:02 +03:00
|
|
|
t.VarType = varTypeCurr
|
2020-04-07 16:34:40 +03:00
|
|
|
} else {
|
|
|
|
return errors.New("invalid TemplateItem")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("invalid TemplateItem")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|