|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/glennliao/apijson-go/model" |
| 6 | +) |
| 7 | + |
| 8 | +type ActionConfig struct { |
| 9 | + requestConfig RequestConfig |
| 10 | + access *Access |
| 11 | + functions *Functions |
| 12 | + rowKeyGenFuncMap map[string]RowKeyGenFuncHandler |
| 13 | + defaultRoleFunc DefaultRole |
| 14 | +} |
| 15 | + |
| 16 | +func (c *ActionConfig) NoVerify() bool { |
| 17 | + return c.access.NoVerify |
| 18 | +} |
| 19 | + |
| 20 | +func (c *ActionConfig) DefaultRoleFunc() DefaultRole { |
| 21 | + return c.defaultRoleFunc |
| 22 | +} |
| 23 | + |
| 24 | +func (c *ActionConfig) GetAccessConfig(key string, noVerify bool) (*AccessConfig, error) { |
| 25 | + return c.access.GetAccess(key, noVerify) |
| 26 | +} |
| 27 | + |
| 28 | +func (c *ActionConfig) CallFunc(ctx context.Context, name string, param model.Map) (any, error) { |
| 29 | + return c.functions.Call(ctx, name, param) |
| 30 | +} |
| 31 | + |
| 32 | +func (c *ActionConfig) GetRequest(tag string, method string, version string) (*Request, error) { |
| 33 | + return c.requestConfig.GetRequest(tag, method, version) |
| 34 | +} |
| 35 | + |
| 36 | +func (c *ActionConfig) RowKeyGen(ctx context.Context, genFuncName string, accessName string, data model.Map) (model.Map, error) { |
| 37 | + if f, exists := c.rowKeyGenFuncMap[genFuncName]; exists { |
| 38 | + req := &RowKeyGenReq{ |
| 39 | + AccessName: accessName, |
| 40 | + Data: data, |
| 41 | + } |
| 42 | + ret := NewRowKeyGenRet() |
| 43 | + err := f(ctx, req, ret) |
| 44 | + return ret.data, err |
| 45 | + } |
| 46 | + |
| 47 | + return nil, nil |
| 48 | +} |
| 49 | + |
| 50 | +// |
| 51 | +//type ExecutorConfig struct { |
| 52 | +// NoVerify bool |
| 53 | +// accessConfig *AccessConfig |
| 54 | +// method string |
| 55 | +// role string |
| 56 | +// DBMeta *DBMeta |
| 57 | +// DbFieldStyle FieldStyle |
| 58 | +// JsonFieldStyle FieldStyle |
| 59 | +//} |
| 60 | +// |
| 61 | +//func NewExecutorConfig(accessConfig *AccessConfig, method string, noVerify bool) *ExecutorConfig { |
| 62 | +// return &ExecutorConfig{ |
| 63 | +// accessConfig: accessConfig, |
| 64 | +// method: method, |
| 65 | +// NoVerify: noVerify, |
| 66 | +// } |
| 67 | +//} |
| 68 | +// |
| 69 | +//func (c *ExecutorConfig) SetRole(role string) { |
| 70 | +// c.role = role |
| 71 | +//} |
| 72 | +// |
| 73 | +//func (c *ExecutorConfig) TableName() string { |
| 74 | +// return c.accessConfig.Name |
| 75 | +//} |
| 76 | +// |
| 77 | +//func (c *ExecutorConfig) TableColumns() []string { |
| 78 | +// return c.DBMeta.GetTableColumns(c.accessConfig.Name) |
| 79 | +//} |
| 80 | +// |
| 81 | +//func (c *ExecutorConfig) GetFieldsGetOutByRole() []string { |
| 82 | +// var fieldsMap map[string]string |
| 83 | +// |
| 84 | +// if val, exists := c.accessConfig.FieldsGet[c.role]; exists { |
| 85 | +// fieldsMap = val.Out |
| 86 | +// } else { |
| 87 | +// fieldsMap = c.accessConfig.FieldsGet["default"].Out |
| 88 | +// } |
| 89 | +// return lo.Keys(fieldsMap) |
| 90 | +//} |
| 91 | +// |
| 92 | +//func (c *ExecutorConfig) GetFieldsGetInByRole() map[string][]string { |
| 93 | +// var inFieldsMap map[string][]string |
| 94 | +// |
| 95 | +// if val, exists := c.accessConfig.FieldsGet[c.role]; exists { |
| 96 | +// inFieldsMap = val.In |
| 97 | +// } else { |
| 98 | +// inFieldsMap = c.accessConfig.FieldsGet["default"].In |
| 99 | +// } |
| 100 | +// |
| 101 | +// return inFieldsMap |
| 102 | +//} |
| 103 | +// |
| 104 | +//func (c *ExecutorConfig) AccessRoles() []string { |
| 105 | +// switch c.method { |
| 106 | +// case http.MethodGet: |
| 107 | +// return c.accessConfig.Get |
| 108 | +// case http.MethodHead: |
| 109 | +// return c.accessConfig.Head |
| 110 | +// case http.MethodPost: |
| 111 | +// return c.accessConfig.Post |
| 112 | +// case http.MethodPut: |
| 113 | +// return c.accessConfig.Put |
| 114 | +// case http.MethodDelete: |
| 115 | +// return c.accessConfig.Delete |
| 116 | +// } |
| 117 | +// return []string{} |
| 118 | +// |
| 119 | +//} |
| 120 | +// |
| 121 | +//func (c *ExecutorConfig) Executor() string { |
| 122 | +// return c.accessConfig.Executor |
| 123 | +// |
| 124 | +//} |
0 commit comments