backend.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package model
  2. import (
  3. "errors"
  4. "sort"
  5. "go.mongodb.org/mongo-driver/bson/primitive"
  6. )
  7. //Backend struct for definition of a backend
  8. type Backend struct {
  9. Backendname string `yaml:"backendname" json:"backendname"`
  10. Description string `yaml:"description" json:"description"`
  11. Models []Model `yaml:"models" json:"models"`
  12. DataSources []DataSource `yaml:"datasources" json:"datasources"`
  13. Rules []Rule `yaml:"rules" json:"rules"`
  14. Destinations []Destination `yaml:"destinations" json:"destinations"`
  15. }
  16. //Model definition of a single model
  17. type Model struct {
  18. Name string `yaml:"name" json:"name"`
  19. Description string `yaml:"description" json:"description"`
  20. Fields []Field `yaml:"fields" json:"fields"`
  21. Indexes []Index `yaml:"indexes" json:"indexes"`
  22. }
  23. //FieldTypeString field type string
  24. const FieldTypeString = "string"
  25. //FieldTypeInt field type integer
  26. const FieldTypeInt = "int"
  27. //FieldTypeFloat field type float
  28. const FieldTypeFloat = "float"
  29. //FieldTypeTime field type time
  30. const FieldTypeTime = "time"
  31. //FieldTypeBool field type bool
  32. const FieldTypeBool = "bool"
  33. //FieldTypeMap field type map
  34. const FieldTypeMap = "map"
  35. //FieldTypeFile field type file
  36. const FieldTypeFile = "file"
  37. //Field definition of a field
  38. type Field struct {
  39. Name string `yaml:"name" json:"name"`
  40. Type string `yaml:"type" json:"type"`
  41. Mandatory bool `yaml:"mandatory" json:"mandatory"`
  42. Collection bool `yaml:"collection" json:"collection"`
  43. }
  44. //Index definition of an index
  45. type Index struct {
  46. Name string `yaml:"name" json:"name"`
  47. Unique bool `yaml:"unique" json:"unique"`
  48. Fields []string `yaml:"fields" json:"fields"`
  49. }
  50. //DataSource Definition of a datasource
  51. type DataSource struct {
  52. Name string `yaml:"name" json:"name"`
  53. Type string `yaml:"type" json:"type"`
  54. Destinations []string `yaml:"destinations" json:"destinations"`
  55. Rule string `yaml:"rule" json:"rule"`
  56. Config interface{} `yaml:"config" json:"config"`
  57. }
  58. type Destination struct {
  59. Name string `yaml:"name" json:"name"`
  60. Type string `yaml:"type" json:"type"`
  61. Config interface{} `yaml:"config" json:"config"`
  62. }
  63. type Rule struct {
  64. Name string `yaml:"name" json:"name"`
  65. Description string `yaml:"description" json:"description"`
  66. Transform interface{} `yaml:"transform" json:"transform"`
  67. }
  68. //ErrModelDefinitionNotFound model definition not found
  69. var ErrModelDefinitionNotFound = errors.New("model defintion not found")
  70. //BackendList list of all definied backends
  71. var BackendList = NewBackends()
  72. //Backends backend list object
  73. type Backends struct {
  74. bs map[string]Backend
  75. }
  76. //NewBackends creating a new Backend list
  77. func NewBackends() Backends {
  78. b := Backends{
  79. bs: make(map[string]Backend),
  80. }
  81. return b
  82. }
  83. //Names getting all backendnames
  84. func (m *Backends) Names() []string {
  85. names := make([]string, 0)
  86. for name := range m.bs {
  87. names = append(names, name)
  88. }
  89. sort.Slice(names, func(i, j int) bool {
  90. return names[i] < names[j]
  91. })
  92. return names
  93. }
  94. //Contains checking if the manufacturer name is present in the list of manufacturers
  95. func (m *Backends) Contains(name string) bool {
  96. for k := range m.bs {
  97. if k == name {
  98. return true
  99. }
  100. }
  101. return false
  102. }
  103. //Add adding a new manufacturer to the list
  104. func (m *Backends) Add(backend Backend) string {
  105. m.bs[backend.Backendname] = backend
  106. return backend.Backendname
  107. }
  108. //Remove remove a single tag
  109. func (m *Backends) Remove(name string) {
  110. if m.Contains(name) {
  111. delete(m.bs, name)
  112. }
  113. }
  114. //Get getting a tag
  115. func (m *Backends) Get(name string) (Backend, bool) {
  116. for k, be := range m.bs {
  117. if k == name {
  118. return be, true
  119. }
  120. }
  121. return Backend{}, false
  122. }
  123. //Clear clearing the list
  124. func (m *Backends) Clear() {
  125. m.bs = make(map[string]Backend)
  126. }
  127. //IsValidDatamodel checking if a data model is valid
  128. func (b *Backend) IsValidDatamodel(model string, data JSONMap) bool {
  129. return true
  130. }
  131. //GetReferencedFiles getting a list of ids of referenced files
  132. func (b *Backend) GetReferencedFiles(modelname string, data JSONMap) ([]string, error) {
  133. model, ok := b.GetModel(modelname)
  134. if !ok {
  135. return nil, ErrModelDefinitionNotFound
  136. }
  137. files := make([]string, 0)
  138. for _, field := range model.Fields {
  139. if field.Type == FieldTypeFile {
  140. dataValue := data[field.Name]
  141. if dataValue != nil {
  142. switch v := dataValue.(type) {
  143. case primitive.A:
  144. values := v
  145. for _, d := range values {
  146. files = append(files, d.(string))
  147. }
  148. case []interface{}:
  149. values := v
  150. for _, d := range values {
  151. files = append(files, d.(string))
  152. }
  153. case []string:
  154. values := v
  155. for _, d := range values {
  156. files = append(files, d)
  157. }
  158. case string:
  159. files = append(files, v)
  160. }
  161. }
  162. }
  163. }
  164. return files, nil
  165. }
  166. //GetModel getting a model definition from the backend definition
  167. func (m *Backends) GetModel(route Route) (Model, bool) {
  168. backend, ok := m.Get(route.Backend)
  169. if !ok {
  170. return Model{}, false
  171. }
  172. return backend.GetModel(route.Model)
  173. }
  174. //GetModel getting a model definition from the backend definition
  175. func (b *Backend) GetModel(modelname string) (Model, bool) {
  176. for _, model := range b.Models {
  177. if model.Name == modelname {
  178. return model, true
  179. }
  180. }
  181. return Model{}, false
  182. }
  183. //GetDatasource getting a datasource definition from the backend definition
  184. func (b *Backend) GetDatasource(datasourcename string) (DataSource, bool) {
  185. for _, datasource := range b.DataSources {
  186. if datasource.Name == datasourcename {
  187. return datasource, true
  188. }
  189. }
  190. return DataSource{}, false
  191. }
  192. //GetDestination getting a datasource definition from the backend definition
  193. func (b *Backend) GetDestination(destinationname string) (Destination, bool) {
  194. for _, destination := range b.Destinations {
  195. if destination.Name == destinationname {
  196. return destination, true
  197. }
  198. }
  199. return Destination{}, false
  200. }
  201. //GetRule getting a rule definition from the backend definition
  202. func (b *Backend) GetRule(rulename string) (Rule, bool) {
  203. for _, rule := range b.Rules {
  204. if rule.Name == rulename {
  205. return rule, true
  206. }
  207. }
  208. return Rule{}, false
  209. }
  210. //GetField getting a field definition from the model definition
  211. func (m *Model) GetField(fieldname string) (Field, bool) {
  212. for _, field := range m.Fields {
  213. if field.Name == fieldname {
  214. return field, true
  215. }
  216. }
  217. return Field{}, false
  218. }
  219. //GetIndex getting a index definition from the model definition
  220. func (m *Model) GetIndex(indexname string) (Index, bool) {
  221. for _, index := range m.Indexes {
  222. if index.Name == indexname {
  223. return index, true
  224. }
  225. }
  226. return Index{}, false
  227. }
  228. //GetFieldNames getting a list of fieldnames from the model definition
  229. func (m *Model) GetFieldNames() []string {
  230. fields := make([]string, 0)
  231. for _, field := range m.Fields {
  232. fields = append(fields, field.Name)
  233. }
  234. return fields
  235. }