xref: /haiku/src/apps/haikudepot/build/scripts/hdsjsonschemacommon.py (revision d1f885b435e9892ac028f4be2b80536b9dd37413)
1
2# =====================================
3# Copyright 2017-2019, Andrew Lindesay
4# Distributed under the terms of the MIT License.
5# =====================================
6
7# common material related to generation of schema-generated artifacts.
8
9import datetime
10
11
12# The possible JSON types
13JSON_TYPE_STRING = "string"
14JSON_TYPE_OBJECT = "object"
15JSON_TYPE_ARRAY = "array"
16JSON_TYPE_BOOLEAN = "boolean"
17JSON_TYPE_INTEGER = "integer"
18JSON_TYPE_NUMBER = "number"
19
20
21# The possible C++ types
22CPP_TYPE_STRING = "BString"
23CPP_TYPE_ARRAY = "List"
24CPP_TYPE_BOOLEAN = "bool"
25CPP_TYPE_INTEGER = "int64"
26CPP_TYPE_NUMBER = "double"
27
28
29def uniondicts(d1, d2):
30    d = dict(d1)
31    d.update(d2)
32    return d
33
34
35def javatypetocppname(javaname):
36    return javaname[javaname.rindex('.')+1:]
37
38
39def propnametocppname(propname):
40    return propname[0:1].upper() + propname[1:]
41
42
43def propnametocppmembername(propname):
44    return 'f' + propnametocppname(propname)
45
46
47def propmetatojsoneventtypename(propmetadata):
48    type = propmetadata['type']
49
50
51
52def propmetadatatocpptypename(propmetadata):
53    type = propmetadata['type']
54
55    if type == JSON_TYPE_STRING:
56        return CPP_TYPE_STRING
57    if type == JSON_TYPE_BOOLEAN:
58        return CPP_TYPE_BOOLEAN
59    if type == JSON_TYPE_INTEGER:
60        return CPP_TYPE_INTEGER
61    if type == JSON_TYPE_NUMBER:
62        return CPP_TYPE_NUMBER
63    if type == JSON_TYPE_OBJECT:
64        javatype = propmetadata['javaType']
65
66        if not javatype or 0 == len(javatype):
67            raise Exception('missing "javaType" field')
68
69        return javatypetocppname(javatype)
70
71    if type == JSON_TYPE_ARRAY:
72        itemsmetadata = propmetadata['items']
73        itemsjavatype = itemsmetadata['javaType']
74
75        if not itemsjavatype or 0 == len(itemsjavatype):
76            raise Exception('missing "javaType" field')
77
78        return "%s <%s*, true>" % (CPP_TYPE_ARRAY, javatypetocppname(itemsjavatype))
79
80    raise Exception('unknown json-schema type [' + type + ']')
81
82
83def propmetadatatypeisscalar(propmetadata):
84    type = propmetadata['type']
85    return type == JSON_TYPE_BOOLEAN or type == JSON_TYPE_INTEGER or type == JSON_TYPE_NUMBER
86
87
88def writetopcomment(f, inputfilename, variant):
89    f.write((
90                '/*\n'
91                ' * Generated %s Object\n'
92                ' * source json-schema : %s\n'
93                ' * generated at : %s\n'
94                ' */\n'
95            ) % (variant, inputfilename, datetime.datetime.now().isoformat()))
96