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 	aermicioi
29 **/
30 module aermicioi.aedi_property_reader.properd.properd;
31 
32 import aermicioi.aedi.storage.storage;
33 import aermicioi.aedi.storage.locator;
34 import aermicioi.aedi_property_reader.core.accessor;
35 import aermicioi.aedi_property_reader.core.convertor;
36 import aermicioi.aedi_property_reader.core.std_conv;
37 import aermicioi.aedi_property_reader.core.document;
38 import aermicioi.aedi_property_reader.core.type_guesser;
39 import lproperd = properd;
40 import std.experimental.logger;
41 import std.experimental.allocator;
42 
43 alias ProperdDocumentContainer = AdvisedDocumentContainer!(string[string], string, StdConvAdvisedConvertor);
44 
45 /**
46 Create a convertor container with data source being properd document.
47 
48 Params:
49     value = source of data for container to use to construct components.
50     accessor = property accessing logic
51     guesser = type guesser based on held properd value
52     allocator = allocator used to allocate converted values
53 Returns:
54     JsonConvertorContainer
55 **/
56 auto properd(string[string] value, PropertyAccessor!(string[string], string) accessor, TypeGuesser!string guesser, RCIAllocator allocator = theAllocator) {
57 
58     ProperdDocumentContainer container = new ProperdDocumentContainer(value);
59     container.guesser = guesser;
60     container.accessor = accessor;
61     container.allocator = allocator;
62 
63     return container;
64 }
65 
66 /**
67 ditto
68 **/
69 auto properd(string[string] value, TypeGuesser!string guesser, RCIAllocator allocator = theAllocator) {
70 
71     return value.properd(accessor, guesser, allocator);
72 }
73 
74 /**
75 ditto
76 **/
77 auto properd(string[string] value, RCIAllocator allocator = theAllocator) {
78     import std.meta : AliasSeq;
79     auto container = value.properd(accessor, new StringToScalarConvTypeGuesser, allocator);
80     import std.datetime;
81     import std.traits;
82 
83     static if (is(StringToScalarConvTypeGuesser: StdConvTypeGuesser!(S, ToTypes), S, ToTypes...)) {
84         static foreach (To; ToTypes) {
85             container.set(StdConvAdvisedConvertor!(To, S)(), fullyQualifiedName!To);
86         }
87     }
88 
89     return container;
90 }
91 
92 /**
93 ditto
94 **/
95 auto properd(RCIAllocator allocator = theAllocator) {
96 
97     return (cast(string[string]) null).properd(allocator);
98 }
99 
100 /**
101 Create a convertor container with data source being properd document.
102 
103 Params:
104     pathOrData = path to a properd file or properd data itself
105     returnEmpty = wheter to return or not a locator with empty data source
106 Returns:
107     properdConvertorContainer
108 **/
109 auto properd(string pathOrData, bool returnEmpty = true) {
110     import std.file;
111     import p = properd;
112 
113     try {
114 
115         if (pathOrData.exists) {
116             return properd(p.readProperties(pathOrData));
117         } else {
118             return properd(p.parseProperties(pathOrData));
119         }
120     } catch (p.PropertyException e) {
121         debug(trace) debug(trace) trace("Error parsing properd: ", e);
122 
123         if (returnEmpty) {
124             debug(trace) debug(trace) trace("Providing empty container");
125             return properd();
126         }
127 
128         throw new Exception("Could not create properd convertor container from file or content passed in pathOrData: " ~ pathOrData, e);
129     }
130 }
131 
132 private auto accessor() {
133     import aermicioi.aedi_property_reader.core.accessor;
134 
135     return new AssociativeArrayAccessor!string;
136 }