xref: /haiku/src/apps/haikudepot/build/scripts/hdsjsonschemacommon.py (revision 372b901dfeada686207d00bbcce456f748bbda12)
1
2# =====================================
3# Copyright 2017-2020, 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 = "BObjectList"
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        itemstype = itemsmetadata['type']
74
75        if not itemstype or 0 == len(itemstype):
76            raise Exception('missing "type" field')
77
78        if itemstype == JSON_TYPE_OBJECT:
79            itemsjavatype = itemsmetadata['javaType']
80            if not itemsjavatype or 0 == len(itemsjavatype):
81                raise Exception('missing "javaType" field')
82            return "%s<%s>" % (CPP_TYPE_ARRAY, javatypetocppname(itemsjavatype))
83
84        if itemstype == JSON_TYPE_STRING:
85            return "%s<%s>" % (CPP_TYPE_ARRAY, CPP_TYPE_STRING)
86
87        raise Exception('unsupported type [%s]' % itemstype)
88
89    raise Exception('unknown json-schema type [' + type + ']')
90
91
92def propmetadatatypeisscalar(propmetadata):
93    type = propmetadata['type']
94    return type == JSON_TYPE_BOOLEAN or type == JSON_TYPE_INTEGER or type == JSON_TYPE_NUMBER
95
96
97def writetopcomment(f, inputfilename, variant):
98    f.write((
99                '/*\n'
100                ' * Generated %s Object\n'
101                ' * source json-schema : %s\n'
102                ' * generated at : %s\n'
103                ' */\n'
104            ) % (variant, inputfilename, datetime.datetime.now().isoformat()))
105