1+ import {
2+ DefinitionDataType ,
3+ DefinitionDataType_Variant ,
4+ DefinitionDataTypeRule
5+ } from "@code0-tech/tucana/pb/shared.data_type_pb.ts" ;
6+ import { getTranslationConnection } from "./helper.ts" ;
7+ import {
8+ DataType ,
9+ DataTypeRule ,
10+ DataTypeRuleConnection , DataTypeRulesContainsKeyConfig ,
11+ DataTypeRulesContainsTypeConfig , DataTypeRulesInputTypesConfig , DataTypeRulesItemOfCollectionConfig ,
12+ DataTypeRulesNumberRangeConfig , DataTypeRulesParentTypeConfig , DataTypeRulesRegexConfig , DataTypeRulesVariant ,
13+ DataTypeVariant
14+ } from "@code0-tech/sagittarius-graphql-types" ;
15+
16+ function mapDataType ( dataType : DefinitionDataType | undefined ) : DataType | null {
17+ if ( dataType == undefined ) {
18+ return null
19+ }
20+
21+ return {
22+ genericKeys : dataType . genericKeys ,
23+ identifier : dataType . identifier ,
24+ name : getTranslationConnection ( dataType . name ) ,
25+ rules : createRules ( dataType . rules ) ,
26+ variant : getDataTypeVariant ( dataType . variant )
27+ }
28+ }
29+
30+ function createRules ( rule : DefinitionDataTypeRule [ ] ) : DataTypeRuleConnection {
31+ return {
32+ count : rule . length ,
33+ nodes : rule . map ( r => {
34+ console . log ( r )
35+ const config : any = r . config
36+ if ( config . ContainsType ) {
37+ const ruleConfig : DataTypeRulesContainsTypeConfig = {
38+ dataTypeIdentifier : null , //TODO
39+ }
40+ const rule : DataTypeRule = {
41+ variant : DataTypeRulesVariant . ContainsType ,
42+ config : ruleConfig
43+ }
44+ return rule ;
45+ }
46+
47+ if ( config . ContainsKey ) {
48+ const ruleConfig : DataTypeRulesContainsKeyConfig = {
49+ dataTypeIdentifier : null , //TODO
50+ key : null ,
51+ }
52+ const rule : DataTypeRule = {
53+ variant : DataTypeRulesVariant . ContainsKey ,
54+ config : ruleConfig
55+ }
56+ return rule ;
57+ }
58+
59+ if ( config . ItemOfCollection ) {
60+ const ruleConfig : DataTypeRulesItemOfCollectionConfig = {
61+ items : null , //TODO
62+ }
63+ const rule : DataTypeRule = {
64+ variant : DataTypeRulesVariant . ItemOfCollection ,
65+ config : ruleConfig
66+ }
67+ return rule ;
68+ }
69+
70+ if ( config . NumberRange ) {
71+ const ruleConfig : DataTypeRulesNumberRangeConfig = {
72+ from : null , //TODO
73+ steps : null , //TODO
74+ to : null , //TODO
75+ }
76+ const rule : DataTypeRule = {
77+ variant : DataTypeRulesVariant . NumberRange ,
78+ config : ruleConfig
79+ }
80+ return rule ;
81+ }
82+
83+ if ( config . Regex ) {
84+ const ruleConfig : DataTypeRulesRegexConfig = {
85+ pattern : null , //TODO
86+ }
87+ const rule : DataTypeRule = {
88+ variant : DataTypeRulesVariant . Regex ,
89+ config : ruleConfig
90+ }
91+ return rule ;
92+ }
93+
94+ if ( config . InputTypes ) {
95+ const ruleConfig : DataTypeRulesInputTypesConfig = {
96+ inputTypes : null , //TODO
97+ }
98+ const rule : DataTypeRule = {
99+ variant : DataTypeRulesVariant . InputType ,
100+ config : ruleConfig
101+ }
102+ return rule ;
103+ }
104+
105+ if ( config . ReturnType ) {
106+ const ruleConfig : DataTypeRulesParentTypeConfig = {
107+ dataTypeIdentifier : null , //TODO
108+ }
109+ const rule : DataTypeRule = {
110+ variant : DataTypeRulesVariant . ReturnType ,
111+ config : ruleConfig
112+ }
113+ return rule ;
114+ }
115+
116+ if ( config . ParentType ) {
117+ const ruleConfig : DataTypeRulesParentTypeConfig = {
118+ dataTypeIdentifier : null , //TODO
119+ }
120+ const rule : DataTypeRule = {
121+ variant : DataTypeRulesVariant . ParentType ,
122+ config : ruleConfig
123+ }
124+ return rule ;
125+ }
126+
127+ throw new Error ( `Unknown rule: ${ rule } ` )
128+ }
129+ )
130+ }
131+ }
132+
133+ function getDataTypeVariant ( variant : DefinitionDataType_Variant ) : DataTypeVariant {
134+ switch ( variant ) {
135+ case DefinitionDataType_Variant . ARRAY :
136+ return DataTypeVariant . Array
137+ case DefinitionDataType_Variant . DATATYPE :
138+ return DataTypeVariant . DataType ;
139+ case DefinitionDataType_Variant . ERROR :
140+ return DataTypeVariant . Error ;
141+ case DefinitionDataType_Variant . NODE :
142+ return DataTypeVariant . Node ;
143+ case DefinitionDataType_Variant . OBJECT :
144+ return DataTypeVariant . Object ;
145+ case DefinitionDataType_Variant . PRIMITIVE :
146+ return DataTypeVariant . Primitive ;
147+ case DefinitionDataType_Variant . TYPE :
148+ return DataTypeVariant . Type ;
149+ default :
150+ throw new Error ( `Unknown variant: ${ variant } ` ) ;
151+ }
152+ }
153+
154+ export { mapDataType }
0 commit comments