|
9 | 9 | {%- macro array(prop) %} |
10 | 10 | {%- if prop.max_count and prop.max_count >= 1 -%} |
11 | 11 | [] |
12 | | -{%- endif %} |
| 12 | +{%- endif -%} |
13 | 13 | {%- endmacro %} |
14 | 14 |
|
15 | | -{%- macro struct_props(class) %} |
16 | | - {%- for parent in class.parent_ids %} |
17 | | - {{ struct_props(classes.get(parent)) }} |
18 | | - {%- endfor %} |
19 | | - |
20 | | - // ---------- {{ type_name(class.clsname) }} -------- |
21 | | - {%- for prop in class.properties %} |
22 | | - {%- if include_prop(classes,class,prop) %} |
23 | | - {{ struct_prop(prop) }} |
24 | | - {%- endif %} |
25 | | - {%- endfor %} |
| 15 | +{%- macro many(prop) %} |
| 16 | +{%- if prop.max_count and prop.max_count >= 1 -%} |
| 17 | +... |
| 18 | +{%- endif -%} |
26 | 19 | {%- endmacro %} |
27 | 20 |
|
28 | | -{%- macro struct_funcs(base,class) %} |
29 | | -{%- for parent in class.parent_ids %} |
30 | | -{{ struct_funcs(base,classes.get(parent)) }} |
31 | | -{%- endfor %} |
32 | | - |
33 | | -// ---------- {{ type_name(class.clsname) }} -------- |
34 | | -{%- for prop in class.properties %} |
35 | | -{%- if include_prop(classes,class,prop) %} |
36 | | - |
37 | | -{{ struct_getter(base,prop) }} |
38 | | - |
39 | | -{{ struct_setter(base,prop) }} |
| 21 | +{%- macro prop_type(prop) %} |
| 22 | +{%- if prop.class_id -%} |
| 23 | +{{ type_name(classes.get(prop.class_id).clsname) }} |
| 24 | +{%- else -%} |
| 25 | +{{ DATATYPES[prop.datatype] }} |
40 | 26 | {%- endif %} |
41 | | -{%- endfor %} |
42 | 27 | {%- endmacro %} |
43 | 28 |
|
44 | | -{%- macro interface_props(class) -%} |
45 | | - {%- for parent in class.parent_ids %} |
46 | | - {{ interface_name(classes.get(parent)) }} |
47 | | - {%- endfor %} |
48 | | - |
49 | | - {%- for prop in class.properties %} |
50 | | - {%- if include_prop(classes,class,prop) %} |
51 | | - |
52 | | -{{ comment(" // ", struct_prop(prop), prop.comment) }} |
53 | | - {{ interface_getter(prop) }} |
| 29 | +{%- macro namedind(class) %} |
| 30 | +type {{ type_name(class.clsname) }} struct { |
| 31 | + ObjRef {{ type_name(class.clsname) }}Ref |
| 32 | +} |
| 33 | +{%- endmacro %} |
54 | 34 |
|
55 | | - {{ interface_setter(prop) }} |
56 | | - {%- endif %} |
57 | | - {%- endfor %} |
| 35 | +{%- macro interface(class) %} |
| 36 | +type {{ type_name(class.clsname) }} interface { |
| 37 | + {% for parent in class.parent_ids %} |
| 38 | + {{ type_name(classes.get(parent).clsname) }} |
| 39 | + {% endfor %} |
| 40 | + {% for prop in class.properties %} |
| 41 | + {{ interface_method(prop.varname) }}() {{ array(prop) }}{{ prop_type(prop) }} |
| 42 | + Set{{ interface_method(prop.varname) }}(p {{ many(prop) }}{{ prop_type(prop) }}) error |
| 43 | + {% endfor %} |
| 44 | +} |
58 | 45 | {%- endmacro %} |
59 | 46 |
|
60 | | -{%- macro interface_constructor(class) %} |
61 | | -func New{{ interface_name(class) }}() {{ interface_name(class) }} { |
62 | | - return &{{ struct_name(class) }}{} |
| 47 | +{%- macro struct(class) %} |
| 48 | +type {{ struct_name(class.clsname) }} struct { |
| 49 | + {% for parent in class.parent_ids %} |
| 50 | + //-----------{{ type_name(classes.get(parent).clsname) }}-------- |
| 51 | + {% for prop in classes.get(parent).properties %} |
| 52 | + {{ struct_prop_name(prop.varname) }} {{ array(prop) }}{{ prop_type(prop) }} |
| 53 | + {% endfor %} |
| 54 | + {% endfor %} |
| 55 | + {% if not class.named_individuals %} |
| 56 | + {% for prop in class.properties %} |
| 57 | + {{ struct_prop_name(prop.varname) }} {{ array(prop) }}{{ prop_type(prop) }} |
| 58 | + {% endfor %} |
| 59 | + {% endif %} |
63 | 60 | } |
64 | 61 | {%- endmacro %} |
65 | 62 |
|
66 | | -{%- macro datatype(prop) %} |
67 | | -{%- if prop.class_id -%} |
68 | | -{{ export(classes.get(prop.class_id).clsname) }} |
69 | | -{%- else -%} |
70 | | -{{ DATATYPES[prop.datatype] }} |
71 | | -{%- endif %} |
| 63 | +{%- macro constructor(class) %} |
| 64 | +func New{{ type_name(class.clsname) }}() {{ type_name(class.clsname) }} { |
| 65 | + return &{{ struct_name(class.clsname) }}{} |
| 66 | +} |
72 | 67 | {%- endmacro %} |
73 | 68 |
|
74 | | -{%- macro interface(class) -%} |
75 | | -type {{ export(class.clsname) }} interface { |
| 69 | +{%- macro func(class) %} |
76 | 70 | {%- for prop in class.properties %} |
77 | | - {{ export(prop.varname) }}() {{ array(prop) }}{{ datatype(prop) }} |
78 | | -{%- endfor %} |
| 71 | +func (o * {{ type_name(class.clsname) }}) {{ interface_method(prop.varname) }}() {{ prop_type(prop) }}{ |
| 72 | + return o.{{ struct_prop_name(prop.varname) }} |
79 | 73 | } |
80 | | -{% endmacro %} |
81 | 74 |
|
82 | | -{%- macro struct(class) -%} |
83 | | -type {{ export(class.clsname) }} struct { |
84 | | -{%- for prop in class.properties %} |
| 75 | +func (o *{{ type_name(class.clsname) }}) Set{{ interface_method(prop.varname) }}(v {{ prop_type(prop) }}) error { |
| 76 | + o.{{ struct_prop_name(prop.varname) }} = v |
| 77 | + return nil |
85 | 78 | } |
86 | 79 | {%- endfor %} |
87 | 80 | {%- endmacro %} |
88 | 81 |
|
89 | 82 | {# go file content #} |
90 | 83 | package spdx_v{{ module }} |
91 | 84 |
|
92 | | -// XSD types that don't have a Go equivalent |
93 | | -type PInt uint |
94 | | - |
95 | | -func (p PInt) Check() PInt { |
96 | | - if p == 0 { |
97 | | - panic("0 Assignment not allowed for PInt") |
98 | | - } |
99 | | - return p |
100 | | -} |
101 | | - |
102 | | -type DateTime string |
103 | | - |
104 | | -type DateTimeStamp string |
105 | | - |
106 | | -// Named Individuals handling |
| 85 | +// NAMED INDIVIDUALS |
107 | 86 |
|
108 | 87 | {%- for class in classes %} |
109 | 88 | {%- if class.named_individuals %} |
110 | | - |
111 | | -type {{ type_name(class.clsname) }} string |
| 89 | +type {{ type_name(class.clsname) }}Ref string |
112 | 90 | var ( |
113 | 91 | {%- for ind in class.named_individuals %} |
114 | | - {{ type_name(class.clsname) }}{{ type_name(ind.varname) }} {{ type_name(class.clsname) }} = "{{ ind._id }}" |
| 92 | + {{ type_name(class.clsname) }}{{ ind.varname }} {{ type_name(class.clsname) }}Ref = "{{ ind._id }}" |
115 | 93 | {%- endfor %} |
116 | 94 | ) |
117 | 95 | {%- endif %} |
118 | 96 | {%- endfor %} |
119 | 97 |
|
120 | | -{% set |
121 | | -DATATYPES = { |
122 | | - "http://www.w3.org/2001/XMLSchema#string": "string", |
123 | | - "http://www.w3.org/2001/XMLSchema#anyURI": "string", |
124 | | - "http://www.w3.org/2001/XMLSchema#integer": "int", |
125 | | - "http://www.w3.org/2001/XMLSchema#positiveInteger": "PInt", |
126 | | - "http://www.w3.org/2001/XMLSchema#nonNegativeInteger": "uint", |
127 | | - "http://www.w3.org/2001/XMLSchema#boolean": "bool", |
128 | | - "http://www.w3.org/2001/XMLSchema#decimal": "float64", |
129 | | - "http://www.w3.org/2001/XMLSchema#dateTime": "DateTime", |
130 | | - "http://www.w3.org/2001/XMLSchema#dateTimeStamp": "DateTimeStamp", |
131 | | -} |
132 | | -%} |
| 98 | +// INTERFACES |
133 | 99 |
|
134 | | -{#var CONTEXT_URLS [{{ context.urls|length }}]string = [{{ context.urls|length }}]string{#} |
135 | | -{# {%- for url in context.urls %}"{{ url }}"{{ ',' if not loop.last }}{%- endfor %}}#} |
136 | | -{##} |
137 | | -{#// CLASSES AND INTERFACES#} |
| 100 | +{% for class in classes %} |
| 101 | +{% if class.named_individuals %} |
| 102 | +{{ namedind(class) }} |
| 103 | +{% else %} |
| 104 | +{{ interface(class) }} |
| 105 | +{% endif %} |
| 106 | +{% endfor %} |
138 | 107 |
|
139 | | -{%- for class in classes %} |
140 | | -{%- if not class.named_individuals %} |
141 | | -type {{ interface_name(class) }} interface { |
142 | | - {{ interface_props(class) }} |
143 | | -} |
144 | | -{%- if not class.is_abstract %} |
145 | | - |
146 | | -type {{ struct_name(class) }} struct { |
147 | | -{#- {%- if class.parent_ids %}#} |
148 | | -{#- {%- for parent in class.parent_ids %}#} |
149 | | -{#- {{ export(classes.get(parent).clsname) }}#} |
150 | | -{#- {%- endfor %}#} |
151 | | -{#- {%- endif %}#} |
152 | | - {{ struct_props(class) }} |
153 | | -{#- {%- if class.named_individuals %}#} |
154 | | -{#- ObjectRef {{ export(class.clsname) }}Ref#} |
155 | | -{#- {%- endif %}#} |
156 | | -} |
| 108 | +// STRUCTS AND CONSTRUCTORS |
157 | 109 |
|
158 | | -{{ interface_constructor(class) }} |
| 110 | +{% for class in concrete_classes %} |
| 111 | +{{ struct(class) }} |
| 112 | +{{ constructor(class) }} |
| 113 | +{% endfor %} |
159 | 114 |
|
160 | | -{{ struct_funcs(class,class) }} |
161 | | -{%- endif %} |
162 | | -{%- endif %} |
163 | | -{%- endfor %} |
| 115 | +// IMPLEMENTATIONS |
| 116 | + |
| 117 | +{% for class in concrete_classes %} |
| 118 | +{{ func(class) }} |
| 119 | +{% endfor %} |
0 commit comments