forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarket.go
58 lines (49 loc) · 1.38 KB
/
market.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package admin
import (
"chat/globals"
"fmt"
"github.com/spf13/viper"
)
type ModelTag []string
type MarketModel struct {
Id string `json:"id" mapstructure:"id" required:"true"`
Name string `json:"name" mapstructure:"name" required:"true"`
Description string `json:"description" mapstructure:"description"`
Default bool `json:"default" mapstructure:"default"`
HighContext bool `json:"high_context" mapstructure:"high_context"`
Avatar string `json:"avatar" mapstructure:"avatar"`
Tag ModelTag `json:"tag" mapstructure:"tag"`
}
type MarketModelList []MarketModel
type Market struct {
Models MarketModelList `json:"models" mapstructure:"models"`
}
func NewMarket() *Market {
var models MarketModelList
if err := viper.UnmarshalKey("market", &models); err != nil {
globals.Warn(fmt.Sprintf("[market] read config error: %s, use default config", err.Error()))
models = MarketModelList{}
}
return &Market{
Models: models,
}
}
func (m *Market) GetModels() MarketModelList {
return m.Models
}
func (m *Market) GetModel(id string) *MarketModel {
for _, model := range m.Models {
if model.Id == id {
return &model
}
}
return nil
}
func (m *Market) SaveConfig() error {
viper.Set("market", m.Models)
return viper.WriteConfig()
}
func (m *Market) SetModels(models MarketModelList) error {
m.Models = models
return m.SaveConfig()
}