|
1 | | -import 'dart:convert'; |
2 | 1 | import 'workflow/node.dart'; |
3 | 2 |
|
4 | 3 | class Workflow{ |
5 | | - final Map<String, dynamic> _json; |
6 | | - Workflow() : _json = jsonDecode(documentJson); |
7 | | - |
8 | | - int get lastNodeId { |
9 | | - if (_json case {'last_node_id': int lastNodeId}) { |
10 | | - return lastNodeId; |
11 | | - } else { |
12 | | - throw const FormatException('Unexpected JSON'); |
13 | | - } |
14 | | - } |
15 | | - |
16 | | - int get lastLinkId { |
17 | | - if (_json case {'last_link_id': int lastLinkId}) { |
18 | | - return lastLinkId; |
19 | | - } else { |
20 | | - throw const FormatException('Unexpected JSON'); |
21 | | - } |
22 | | - } |
23 | | - |
24 | | - List<dynamic> get nodes { |
25 | | - if (_json case {'nodes': List<dynamic> nodes}) { |
26 | | - return nodes.map((node) => WorkflowNode.fromJSON(node)).toList(); |
27 | | - } else { |
28 | | - throw const FormatException('Unexpected JSON'); |
29 | | - } |
30 | | - } |
31 | | - |
32 | | - List<dynamic> get links { |
33 | | - if (_json case {'links': List<dynamic> links}) { |
34 | | - return links; |
35 | | - } else { |
36 | | - throw const FormatException('Unexpected JSON'); |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - List<dynamic> get groups { |
41 | | - if (_json case {'groups': List<dynamic> links}) { |
42 | | - return links; |
43 | | - } else { |
44 | | - throw const FormatException('Unexpected JSON'); |
45 | | - } |
| 4 | + final int lastNodeId; |
| 5 | + final int lastLinkId; |
| 6 | + final List<WorkflowNode> nodes; |
| 7 | + final Set<dynamic> links; |
| 8 | + final List<dynamic> groups; |
| 9 | + final Map config; |
| 10 | + final Map<String, dynamic> extra; |
| 11 | + final String version; |
| 12 | + |
| 13 | + Workflow( |
| 14 | + this.lastNodeId, |
| 15 | + this.lastLinkId, |
| 16 | + this.nodes, |
| 17 | + this.links, |
| 18 | + this.groups, |
| 19 | + this.config, |
| 20 | + this.extra, |
| 21 | + this.version |
| 22 | + ); |
| 23 | + |
| 24 | + factory Workflow.fromJSON( Map<String, dynamic> _json) { |
| 25 | + final lastNodeId = _json['last_node_id'] as int; |
| 26 | + final lastLinkId = _json['last_link_id'] as int; |
| 27 | + final nodes = _json['nodes'].map<WorkflowNode>((node) => WorkflowNode.fromJSON(node)).toList(); |
| 28 | + final links = _json['links'].map((prop) => prop is String ? prop as int : prop as String).toSet(); |
| 29 | + final groups = _json['groups'].map((group) => group as Map<String, dynamic>).toList() as List<dynamic>; |
| 30 | + final config = _json['config'] as Map; |
| 31 | + final extra = _json['extra'] as Map<String, dynamic>; |
| 32 | + final version = "${_json['version']}"; |
| 33 | + |
| 34 | + return Workflow( |
| 35 | + lastNodeId, |
| 36 | + lastLinkId, |
| 37 | + nodes, |
| 38 | + links, |
| 39 | + groups, |
| 40 | + config, |
| 41 | + extra, |
| 42 | + version |
| 43 | + ); |
46 | 44 | } |
47 | | - |
48 | | - Map get config { |
49 | | - if (_json case {'config': Map config}) { |
50 | | - return config; |
51 | | - } else { |
52 | | - throw const FormatException('Unexpected JSON'); |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - List<dynamic> get extra { |
57 | | - if (_json case {'extra': List<dynamic> extra}) { |
58 | | - return extra; |
59 | | - } else { |
60 | | - throw const FormatException('Unexpected JSON'); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - String get version { |
65 | | - if (_json case {'version': String version}) { |
66 | | - return version; |
67 | | - } else { |
68 | | - throw const FormatException('Unexpected JSON'); |
69 | | - } |
70 | | - } |
71 | | -} |
72 | | - |
73 | | -const documentJson = ''' |
74 | | -{ |
75 | | - "last_node_id": 1, |
76 | | - "last_link_id": 1, |
77 | | - "nodes": [], |
78 | | - "links": [], |
79 | | - "groups": [], |
80 | | - "config": {}, |
81 | | - "extra": {}, |
82 | | - "version": 0.4 |
83 | 45 | } |
84 | | -'''; |
0 commit comments