1818// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919// SOFTWARE.
2020
21- export type Type = BaseType ;
21+ export type Type =
22+ | BaseType
23+ | TupleType
24+ | OrType
25+ | AndType
26+ | ReferenceType
27+ | ArrayType
28+ | MapType
29+ | StringLiteralType
30+ | LiteralType ;
2231
23- export type BaseTypeName = "string" | "integer" | "uinteger" ;
32+ export type BaseTypeName = "null" | " string" | "integer" | "uinteger" | "decimal" | "boolean" | "DocumentUri" | "URI ";
2433
2534export type BaseType = {
2635 kind : "base" ;
2736 name : BaseTypeName ;
2837} ;
2938
39+ export type TupleType = {
40+ kind : "tuple" ;
41+ items : Type [ ] ;
42+ } ;
43+
44+ export type OrType = {
45+ kind : "or" ;
46+ items : Type [ ] ;
47+ } ;
48+
49+ export type AndType = {
50+ kind : "and" ;
51+ items : Type [ ] ;
52+ } ;
53+
54+ export type ReferenceType = {
55+ kind : "reference" ;
56+ name : string ;
57+ } ;
58+
59+ export type ArrayType = {
60+ kind : "array" ;
61+ element : Type ;
62+ } ;
63+
64+ export type MapType = {
65+ kind : "map" ;
66+ key : Type ;
67+ value : Type ;
68+ } ;
69+
70+ export type StringLiteralType = {
71+ kind : "stringLiteral" ;
72+ value : string ;
73+ } ;
74+
75+ export type LiteralType = {
76+ kind : "literal" ;
77+ value : {
78+ properties : unknown [ ] ;
79+ } ;
80+ } ;
81+
3082export type EnumerationValue = {
3183 name : string ;
3284 value : string ;
@@ -47,9 +99,25 @@ export type Notification = {};
4799
48100export type Request = { } ;
49101
50- export type Structure = { } ;
102+ export type Property = {
103+ documentation ?: string ;
104+ name : string ;
105+ type : Type ;
106+ optional ?: boolean ;
107+ } ;
108+
109+ export type Structure = {
110+ extends ?: ReferenceType [ ] ;
111+ mixins ?: ReferenceType [ ] ;
112+ name : string ;
113+ properties : Property [ ] ;
114+ } ;
51115
52- export type TypeAlias = { } ;
116+ export type TypeAlias = {
117+ documentation ?: string ;
118+ name : string ;
119+ type : Type ;
120+ } ;
53121
54122export type MetaModel = {
55123 metaData : MetaData ;
@@ -65,7 +133,7 @@ export function getEnumBaseType(enumeration: Enumeration) {
65133 case "integer" :
66134 return " : int" ;
67135 case "uinteger" :
68- return " : unsigned int " ;
136+ return " : long " ;
69137 default :
70138 return "" ;
71139 }
@@ -80,12 +148,117 @@ export function getEnumeratorName(enumerator: EnumerationValue) {
80148 return `k${ name } ` ;
81149}
82150
83- export function getEnumeratorInitializer (
84- enumeration : Enumeration ,
85- enumerator : EnumerationValue
86- ) {
151+ export function getEnumeratorInitializer ( enumeration : Enumeration , enumerator : EnumerationValue ) {
87152 if ( enumeration . type . name === "string" ) {
88153 return "" ;
89154 }
90155 return ` = ${ enumerator . value } ` ;
91156}
157+
158+ export function toCppType ( type : Type ) : string {
159+ switch ( type . kind ) {
160+ case "base" : {
161+ switch ( type . name ) {
162+ case "null" :
163+ return "std::nullptr_t" ;
164+ case "string" :
165+ return "std::string" ;
166+ case "integer" :
167+ return "int" ;
168+ case "uinteger" :
169+ return "long" ;
170+ case "decimal" :
171+ return "double" ;
172+ case "boolean" :
173+ return "bool" ;
174+ case "DocumentUri" :
175+ return "std::string" ;
176+ case "URI" :
177+ return "std::string" ;
178+ default :
179+ throw new Error ( `Unknown base type: ${ JSON . stringify ( type ) } ` ) ;
180+ } // switch type.name
181+ } // case "base"
182+
183+ case "stringLiteral" :
184+ return "std::string" ;
185+
186+ case "literal" :
187+ return "json" ;
188+
189+ case "reference" :
190+ return type . name ;
191+
192+ case "array" :
193+ return `Vector<${ toCppType ( type . element ) } >` ;
194+
195+ case "map" :
196+ return `Map<${ toCppType ( type . key ) } , ${ toCppType ( type . value ) } >` ;
197+
198+ case "tuple" :
199+ return `std::tuple<${ type . items . map ( toCppType ) . join ( ", " ) } >` ;
200+
201+ case "or" :
202+ return `std::variant<std::monostate, ${ type . items . map ( toCppType ) . join ( ", " ) } >` ;
203+
204+ case "and" :
205+ return `std::tuple<${ type . items . map ( toCppType ) . join ( ", " ) } >` ;
206+
207+ default :
208+ throw new Error ( `Unknown type kind: ${ JSON . stringify ( type ) } ` ) ;
209+ } // switch
210+ }
211+
212+ export function getStructureProperties ( model : MetaModel , structure : Structure ) : Property [ ] {
213+ const structByName = new Map ( model . structures . map ( ( s ) => [ s . name , s ] ) ) ;
214+ const added = new Set < string > ( ) ;
215+ return getStructurePropertiesHelper ( { structure, added, structByName } ) ;
216+ }
217+
218+ function getStructurePropertiesHelper ( {
219+ structure,
220+ added,
221+ structByName,
222+ } : {
223+ structure : Structure ;
224+ added : Set < string > ;
225+ structByName : Map < string , Structure > ;
226+ } ) : Property [ ] {
227+ const properties : Property [ ] = [ ] ;
228+
229+ for ( const property of structure . properties ) {
230+ if ( added . has ( property . name ) ) {
231+ continue ;
232+ }
233+ added . add ( property . name ) ;
234+ properties . push ( property ) ;
235+ }
236+
237+ structure . extends ?. forEach ( ( ref ) => {
238+ const extend = structByName . get ( ref . name ) ;
239+
240+ if ( ! extend ) {
241+ throw new Error ( `Unknown extends ${ ref . name } ` ) ;
242+ }
243+
244+ properties . push (
245+ ...getStructurePropertiesHelper ( {
246+ structure : extend ,
247+ added,
248+ structByName,
249+ } ) ,
250+ ) ;
251+ } ) ;
252+
253+ structure . mixins ?. forEach ( ( ref ) => {
254+ const mixin = structByName . get ( ref . name ) ;
255+
256+ if ( ! mixin ) {
257+ throw new Error ( `Unknown mixin ${ ref . name } ` ) ;
258+ }
259+
260+ properties . push ( ...getStructurePropertiesHelper ( { structure : mixin , added, structByName } ) ) ;
261+ } ) ;
262+
263+ return properties ;
264+ }
0 commit comments