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.

83 lines
2.1 KiB

3 years ago
3 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, model_name, model_attrs, template, label_mapping, task_type }:
  38. { id: number, model_name: string, model_attrs: object, template: string, label_mapping: object, task_type: string }
  39. ): ConfigItem {
  40. return new ConfigItem(id, model_name, model_attrs, template, label_mapping, task_type)
  41. }
  42. static parseFromUI(
  43. { modelName, modelAttrs, template, labelMapping, taskType }: Fields): ConfigItem {
  44. const mapping = labelMapping.reduce((a, x) => ({...a, [x.from]: x.to}), {})
  45. const attributes: {[key: string]: any} = modelAttrs.reduce((a, x) => ({...a, [x.name]: x.value}), {})
  46. for (const [key, value] of Object.entries(attributes)) {
  47. if (Array.isArray(value)) {
  48. attributes[key] = value.reduce((a, x) => ({...a, [x.key]: x.value}), {})
  49. }
  50. }
  51. return new ConfigItem(99999, modelName, attributes, template, mapping, taskType)
  52. }
  53. toObject(): object {
  54. return {
  55. id: this.id,
  56. modelName: this.modelName,
  57. modelAttrs: this.modelAttrs,
  58. template: this.template,
  59. labelMapping: this.labelMapping,
  60. taskType: this.taskType
  61. }
  62. }
  63. toAPI(): object {
  64. return {
  65. id: this.id,
  66. model_name: this.modelName,
  67. model_attrs: this.modelAttrs,
  68. template: this.template,
  69. label_mapping: this.labelMapping,
  70. task_type: this.taskType
  71. }
  72. }
  73. }