11class_name JSONProperty
22
33
4- const PRECISION_ERROR = 0.00001
4+ const PRECISION_ERROR = 0.000001
55
66
77enum Types {
@@ -20,6 +20,49 @@ enum Types {
2020}
2121
2222
23+ const MESSAGE_BOOL = "boolean"
24+ const MESSAGE_NUMBER = "number"
25+ const MESSAGE_INTEGER = "integer"
26+ const MESSAGE_PERCENTAGE = "percentage"
27+ const MESSAGE_STRING = "string"
28+ const MESSAGE_ENUM = "string"
29+ const MESSAGE_ARRAY = "array"
30+ const MESSAGE_COLOR = "color"
31+ const MESSAGE_OBJECT = "object"
32+ const MESSAGE_FILE = "file path"
33+ const MESSAGE_JSON_CONFIG_FILE = "JSON config file path"
34+ const MESSAGE_IMAGE = "image path"
35+
36+
37+ static func _type_as_text (type : int ) -> String :
38+ match type :
39+ Types .BOOL :
40+ return MESSAGE_BOOL
41+ Types .NUMBER :
42+ return MESSAGE_NUMBER
43+ Types .INTEGER :
44+ return MESSAGE_INTEGER
45+ Types .PERCENTAGE :
46+ return MESSAGE_PERCENTAGE
47+ Types .STRING :
48+ return MESSAGE_STRING
49+ Types .ENUM :
50+ return MESSAGE_ENUM
51+ Types .ARRAY :
52+ return MESSAGE_ARRAY
53+ Types .COLOR :
54+ return MESSAGE_COLOR
55+ Types .OBJECT :
56+ return MESSAGE_OBJECT
57+ Types .FILE :
58+ return MESSAGE_FILE
59+ Types .JSON_CONFIG_FILE :
60+ return MESSAGE_JSON_CONFIG_FILE
61+ Types .IMAGE :
62+ return MESSAGE_IMAGE
63+ _ :
64+ return "This type message is not defined"
65+
2366enum Errors {
2467 COULD_NOT_OPEN_FILE ,
2568 COULD_NOT_OPEN_IMAGE ,
@@ -49,11 +92,149 @@ enum Errors {
4992}
5093
5194
95+ const MESSAGE_COULD_NOT_OPEN_FILE = "Could not open the file"
96+ const MESSAGE_COULD_NOT_OPEN_IMAGE = "Could not open the image"
97+ const MESSAGE_EMPTY_FILE = "The configuration file can not be empty"
98+ const MESSAGE_JSON_PARSING_ERROR = "JSON parsing error at line %d : \" %s \" "
99+ const MESSAGE_WRONG_TYPE = "Wrong type: expected '%s '"
100+ const MESSAGE_NUMBER_VALUE_LESS_THAN_MIN = "%.3f is less than the minimum allowed (%.3f )"
101+ const MESSAGE_NUMBER_VALUE_MORE_THAN_MAX = "%.3f is more than the maximum allowed (%.3f )"
102+ const MESSAGE_INTEGER_VALUE_LESS_THAN_MIN = "%d is less than the minimum allowed (%d )"
103+ const MESSAGE_INTEGER_VALUE_MORE_THAN_MAX = "%d is more than the maximum allowed (%d )"
104+ const MESSAGE_PERCENTAGE_LESS_THAN_ZERO = "%.3f is less than 0"
105+ const MESSAGE_PERCENTAGE_MORE_THAN_ONE = "%.3f is more than 1"
106+ const MESSAGE_STRING_SHORTER_THAN_MIN = "The string length (%s ) is shorter than the minimum length allowed (%d )"
107+ const MESSAGE_STRING_LONGER_THAN_MAX = "The string length (%s ) is longer than the maximum length allowed (%d )"
108+ const MESSAGE_STRING_DO_NOT_MATCH_PATTERN = "'%s ' does not match the specified pattern (%s )"
109+ const MESSAGE_ENUM_NOT_VALID = "'%s ' is not in the list of valid values"
110+ const MESSAGE_ARRAY_SMALLER_THAN_MIN = "The array size (%d ) is smaller than the minimum allowed (%d )"
111+ const MESSAGE_ARRAY_BIGGER_THAN_MAX = "The array size (%d ) is bigger than the maximum allowed (%d )"
112+ const MESSAGE_ARRAY_TWO_ELEMENTS_ARE_EQUAL = "The array contains two elements that are equal: [%d ] and [%d ]"
113+ const MESSAGE_COLOR_WRONG_SIZE = "The color is %d element(s) long, when it should be 3 to 4"
114+ const MESSAGE_COLOR_WRONG_TYPE = "Wrong type: expected 'integer' in the range [0, 255]"
115+ const MESSAGE_COLOR_OUT_OF_RANGE = "Element out of the range [0, 255]"
116+ const MESSAGE_OBJECT_MISSING_PROPERTY = "The property '%s ' has not been specified"
117+ const MESSAGE_OBJECT_NON_VALID_PROPERTY = "The property '%s ' is not a valid one"
118+ const MESSAGE_OBJECT_ONE_IS_REQUIRED = "One of this properties needs to be specified: %s "
119+ const MESSAGE_OBJECT_EXCLUSIVITY_ERROR = "This properties can not be present at the same time: %s "
120+ const MESSAGE_OBJECT_DEPENDENCY_ERROR = "'%s ' property has been specified, but '%s ' is missing"
121+ const MESSAGE_IMAGE_WRONG_SIZE = "The image is not the correct size (%d , %d )"
122+
123+ const MESSAGE_WITH_CONTEXT = ", at '%s '."
124+ const MESSAGE_WITHOUT_CONTEXT = "."
125+
126+
127+ static func _array_as_text (array : Array ) -> String :
128+ var array_as_text = ""
129+
130+ for i in array .size ():
131+ if i == 0 :
132+ array_as_text = String (array [0 ])
133+ else :
134+ array_as_text = array_as_text + ", " + String (array [i ])
135+
136+ return array_as_text
137+
138+
139+ static func _error_as_text (error : Dictionary ) -> String :
140+ var error_as_text
141+
142+ if error .has ("error" ):
143+ match error .error :
144+ Errors .COULD_NOT_OPEN_FILE :
145+ error_as_text = MESSAGE_COULD_NOT_OPEN_FILE
146+ Errors .COULD_NOT_OPEN_IMAGE :
147+ error_as_text = MESSAGE_COULD_NOT_OPEN_IMAGE
148+ Errors .EMPTY_FILE :
149+ error_as_text = MESSAGE_EMPTY_FILE
150+ Errors .JSON_PARSING_ERROR :
151+ error_as_text = MESSAGE_JSON_PARSING_ERROR % [error .line , error .string ]
152+ Errors .WRONG_TYPE :
153+ error_as_text = MESSAGE_WRONG_TYPE % _type_as_text (error .expected )
154+ Errors .NUMBER_VALUE_LESS_THAN_MIN :
155+ if typeof (error .value ) == TYPE_REAL :
156+ error_as_text = MESSAGE_NUMBER_VALUE_LESS_THAN_MIN % [error .value , error .min ]
157+ else :
158+ error_as_text = MESSAGE_INTEGER_VALUE_LESS_THAN_MIN % [error .value , error .min ]
159+ Errors .NUMBER_VALUE_MORE_THAN_MAX :
160+ if typeof (error .value ) == TYPE_REAL :
161+ error_as_text = MESSAGE_NUMBER_VALUE_MORE_THAN_MAX % [error .value , error .max ]
162+ else :
163+ error_as_text = MESSAGE_INTEGER_VALUE_MORE_THAN_MAX % [error .value , error .max ]
164+ Errors .PERCENTAGE_LESS_THAN_ZERO :
165+ error_as_text = MESSAGE_PERCENTAGE_LESS_THAN_ZERO % error .value
166+ Errors .PERCENTAGE_MORE_THAN_ONE :
167+ error_as_text = MESSAGE_PERCENTAGE_MORE_THAN_ONE % error .value
168+ Errors .STRING_SHORTER_THAN_MIN :
169+ error_as_text = MESSAGE_STRING_SHORTER_THAN_MIN % [error .length , error .min ]
170+ Errors .STRING_LONGER_THAN_MAX :
171+ error_as_text = MESSAGE_STRING_LONGER_THAN_MAX % [error .length , error .max ]
172+ Errors .STRING_DO_NOT_MATCH_PATTERN :
173+ error_as_text = MESSAGE_STRING_DO_NOT_MATCH_PATTERN % [error .value , error .pattern ]
174+ Errors .ENUM_NOT_VALID :
175+ error_as_text = MESSAGE_ENUM_NOT_VALID % error .value
176+ Errors .ARRAY_SMALLER_THAN_MIN :
177+ error_as_text = MESSAGE_ARRAY_SMALLER_THAN_MIN % [error .size , error .min ]
178+ Errors .ARRAY_BIGGER_THAN_MAX :
179+ error_as_text = MESSAGE_ARRAY_BIGGER_THAN_MAX % [error .size , error .max ]
180+ Errors .ARRAY_TWO_ELEMENTS_ARE_EQUAL :
181+ error_as_text = MESSAGE_ARRAY_TWO_ELEMENTS_ARE_EQUAL % [error .element_1 , error .element_2 ]
182+ Errors .COLOR_WRONG_SIZE :
183+ error_as_text = MESSAGE_COLOR_WRONG_SIZE % error .size
184+ Errors .COLOR_WRONG_TYPE :
185+ error_as_text = MESSAGE_COLOR_WRONG_TYPE
186+ Errors .COLOR_OUT_OF_RANGE :
187+ error_as_text = MESSAGE_COLOR_OUT_OF_RANGE
188+ Errors .OBJECT_MISSING_PROPERTY :
189+ error_as_text = MESSAGE_OBJECT_MISSING_PROPERTY % error .property
190+ Errors .OBJECT_NON_VALID_PROPERTY :
191+ error_as_text = MESSAGE_OBJECT_NON_VALID_PROPERTY % error .property
192+ Errors .OBJECT_ONE_IS_REQUIRED :
193+ error_as_text = MESSAGE_OBJECT_ONE_IS_REQUIRED % _array_as_text (error .properties )
194+ Errors .OBJECT_EXCLUSIVITY_ERROR :
195+ error_as_text = MESSAGE_OBJECT_EXCLUSIVITY_ERROR % _array_as_text (error .properties )
196+ Errors .OBJECT_DEPENDENCY_ERROR :
197+ error_as_text = MESSAGE_OBJECT_DEPENDENCY_ERROR % [error .main_property , error .dependent_property ]
198+ Errors .IMAGE_WRONG_SIZE :
199+ error_as_text = MESSAGE_IMAGE_WRONG_SIZE % [error .expected_size [0 ], error .expected_size [1 ]]
200+ _ :
201+ error_as_text = "This error message is not defined"
202+ else :
203+ error_as_text = "This error message is not defined"
204+
205+ if error .has ("context" ):
206+ error_as_text = error_as_text + MESSAGE_WITH_CONTEXT % error .context
207+ else :
208+ error_as_text = error_as_text + MESSAGE_WITHOUT_CONTEXT
209+
210+ return error_as_text
211+
212+
52213enum Warnings {
53214 IMAGE_WRONG_SIZE ,
54215}
55216
56217
218+ static func _warning_as_text (warning : Dictionary ) -> String :
219+ var warning_as_text
220+
221+ if warning .has ("warning" ):
222+ match warning .warning :
223+ Warnings .IMAGE_WRONG_SIZE :
224+ warning_as_text = MESSAGE_IMAGE_WRONG_SIZE % [warning .expected_size [0 ], warning .expected_size [1 ]]
225+ _ :
226+ warning_as_text = "This warning message is not defined"
227+ else :
228+ warning_as_text = "This warning message is not defined"
229+
230+ if warning .has ("context" ):
231+ warning_as_text = warning_as_text + MESSAGE_WITH_CONTEXT % warning .context
232+ else :
233+ warning_as_text = warning_as_text + MESSAGE_WITHOUT_CONTEXT
234+
235+ return warning_as_text
236+
237+
57238var _result
58239var _errors := []
59240var _warnings := []
0 commit comments