1 /**
2 License:
3     Boost Software License - Version 1.0 - August 17th, 2003
4 
5     Permission is hereby granted, free of charge, to any person or organization
6     obtaining a copy of the software and accompanying documentation covered by
7     this license (the "Software") to use, reproduce, display, distribute,
8     execute, and transmit the Software, and to prepare derivative works of the
9     Software, and to permit third-parties to whom the Software is furnished to
10     do so, all subject to the following:
11 
12     The copyright notices in the Software and this entire statement, including
13     the above license grant, this restriction and the following disclaimer,
14     must be included in all copies of the Software, in whole or in part, and
15     all derivative works of the Software, unless such copies or derivative
16     works are solely in the form of machine-executable object code generated by
17     a source language processor.
18 
19     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21     FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22     SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23     FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25     DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28     Alexandru Ermicioi
29 **/
30 module aermicioi.aedi_property_reader.sdlang.inspector;
31 
32 import aermicioi.aedi_property_reader.core.inspector;
33 import sdlang;
34 import sdlang.ast;
35 import std.algorithm;
36 import aermicioi.aedi_property_reader.sdlang.accessor;
37 import aermicioi.aedi.exception : NotFoundException;
38 
39 /**
40 Inspector for sdlang tags.
41 **/
42 class SdlangTagInspector : Inspector!Tag {
43 
44     /**
45     Identify the type of child field of component.
46 
47     Params:
48         component = a composite component (class, struct, assoc array etc.) containing some fields
49 
50     Returns:
51         Type of field, or typeid(void) if field is not present in component
52     **/
53     TypeInfo typeOf(Tag component, in string property) const nothrow {
54         try {
55             auto fields = component.tags.filter!(c => c.name == property);
56 
57             if (!fields.empty) {
58                 return typeid(Tag);
59             }
60 
61             auto attributes = component.attributes.filter!(a => a.name == property);
62 
63             if (!attributes.empty) {
64                 return typeid(Attribute);
65             }
66         } catch (Exception e) {
67 
68         }
69 
70         return typeid(void);
71     }
72 
73     /**
74     Identify the type of component itself.
75 
76     Identify the type of component itself. It will inspect the component and will return accurate
77     type info that the component represents.
78 
79     Params:
80         component = component which should be identified.
81 
82     Returns:
83         Type info of component, or typeid(void) if component cannot be identified by inspector
84     **/
85     TypeInfo typeOf(Tag component) const nothrow {
86         return typeid(component);
87     }
88 
89     /**
90     Check if component has a field or a property.
91 
92     Params:
93         component = component with fields
94         property = component property that is tested for existence
95 
96     Returns:
97         true if field is present either in readonly, or writeonly form (has getters and setters).
98     **/
99     bool has(Tag component, in string property) const nothrow {
100         try {
101 
102             auto fields = component.tags.filter!(c => c.name == property);
103 
104             if (!fields.empty) {
105                 return true;
106             }
107 
108             auto attributes = component.attributes.filter!(a => a.name == property);
109 
110             if (!attributes.empty) {
111                 return true;
112             }
113         } catch (Exception e) {
114 
115         }
116 
117         return false;
118     }
119 
120     /**
121     Return a list of properties that component holds.
122 
123     Params:
124         component = the component with fields
125 
126     Returns:
127         an arary of property identities.
128     **/
129     string[] properties(Tag component) const nothrow {
130         try {
131             import std.range : only, chain;
132             import std.array : array;
133             return chain(component.tags.map!(c => c.name), component.attributes.map!(c => c.name)).array;
134         } catch (Exception e) {
135 
136         }
137 
138         return [];
139     }
140 }