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.

102 lines
2.2 KiB

2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. export class ConfigItemList {
  2. constructor(public configItems: ConfigItem[]) {}
  3. static valueOf(items: ConfigItem[]): ConfigItemList {
  4. return new ConfigItemList(items)
  5. }
  6. toArray(): Object[] {
  7. return this.configItems.map((item) => item.toObject())
  8. }
  9. }
  10. interface LabelMappingForUI {
  11. from: string
  12. to: string
  13. }
  14. export interface ParametersForUI {
  15. name: string
  16. value: string | object[]
  17. type?: string
  18. items?: string[]
  19. }
  20. export interface Fields {
  21. modelName: string
  22. modelAttrs: ParametersForUI[]
  23. template: string
  24. labelMapping: LabelMappingForUI[]
  25. taskType: string
  26. }
  27. export class ConfigItem {
  28. constructor(
  29. public id: number,
  30. public modelName: string,
  31. public modelAttrs: object,
  32. public template: string,
  33. public labelMapping: object,
  34. public taskType: string
  35. ) {}
  36. static valueOf({
  37. id,
  38. model_name,
  39. model_attrs,
  40. template,
  41. label_mapping,
  42. task_type
  43. }: {
  44. id: number
  45. model_name: string
  46. model_attrs: object
  47. template: string
  48. label_mapping: object
  49. task_type: string
  50. }): ConfigItem {
  51. return new ConfigItem(id, model_name, model_attrs, template, label_mapping, task_type)
  52. }
  53. static parseFromUI({
  54. modelName,
  55. modelAttrs,
  56. template,
  57. labelMapping,
  58. taskType
  59. }: Fields): ConfigItem {
  60. const mapping = labelMapping.reduce((a, x) => ({ ...a, [x.from]: x.to }), {})
  61. const attributes: { [key: string]: any } = modelAttrs.reduce(
  62. (a, x) => ({ ...a, [x.name]: x.value }),
  63. {}
  64. )
  65. for (const [key, value] of Object.entries(attributes)) {
  66. if (Array.isArray(value)) {
  67. attributes[key] = value.reduce((a, x) => ({ ...a, [x.key]: x.value }), {})
  68. }
  69. }
  70. return new ConfigItem(99999, modelName, attributes, template, mapping, taskType)
  71. }
  72. toObject(): object {
  73. return {
  74. id: this.id,
  75. modelName: this.modelName,
  76. modelAttrs: this.modelAttrs,
  77. template: this.template,
  78. labelMapping: this.labelMapping,
  79. taskType: this.taskType
  80. }
  81. }
  82. toAPI(): object {
  83. return {
  84. id: this.id,
  85. model_name: this.modelName,
  86. model_attrs: this.modelAttrs,
  87. template: this.template,
  88. label_mapping: this.labelMapping,
  89. task_type: this.taskType
  90. }
  91. }
  92. }