11/*
22 * Copyright (c) 2025 Carter Games
3- *
3+ *
44 * Permission is hereby granted, free of charge, to any person obtaining a copy
55 * of this software and associated documentation files (the "Software"), to deal
66 * in the Software without restriction, including without limitation the rights
77 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88 * copies of the Software, and to permit persons to whom the Software is
99 * furnished to do so, subject to the following conditions:
10- *
10+ *
1111 * The above copyright notice and this permission notice shall be included in
1212 * all copies or substantial portions of the Software.
13- *
14- *
13+ *
14+ *
1515 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -37,22 +37,18 @@ namespace CarterGames.NotionData
3737 public static class NotionPropertyValueHandler
3838 {
3939 /// <summary>
40- /// Gets the value in this property its type if possible.
40+ /// Gets the value in this property its type if possible.
4141 /// </summary>
4242 /// <param name="property">The notion property to read from.</param>
4343 /// <param name="fieldType">The field type to match.</param>
4444 /// <param name="value">The object value assigned.</param>
4545 /// <returns>If the parsing was successful.</returns>
4646 public static bool TryGetValueAs ( NotionProperty property , Type fieldType , out object value )
4747 {
48- Debug . Log ( fieldType ) ;
49- Debug . Log ( fieldType . IsArray ) ;
50-
51-
5248 if ( fieldType . IsArray )
5349 {
5450 if ( TryParseAsArray ( property , fieldType , out value ) ) return true ;
55-
51+
5652 if ( fieldType . IsEnum )
5753 {
5854 if ( TryParseEnumOrEnumFlags ( property , fieldType , out value ) ) return true ;
@@ -62,40 +58,39 @@ public static bool TryGetValueAs(NotionProperty property, Type fieldType, out ob
6258 if ( fieldType . IsGenericList ( ) )
6359 {
6460 if ( TryParseAsList ( property , fieldType , out value ) ) return true ;
65-
61+
6662 if ( fieldType . IsEnum )
6763 {
6864 if ( TryParseEnumOrEnumFlags ( property , fieldType , out value ) ) return true ;
6965 }
7066 }
71-
72-
67+
68+
7369 if ( TryParseAsPrimitive ( property , fieldType , out value ) ) return true ;
74-
75-
70+
71+
7672 if ( fieldType . IsEnum )
7773 {
7874 if ( TryParseEnumOrEnumFlags ( property , fieldType , out value ) ) return true ;
7975 }
80-
81-
76+
77+
8278 if ( TryParseWrapper ( property , fieldType , out value ) ) return true ;
83-
84- Debug . Log ( property . PropertyName ) ;
85-
79+
8680 if ( fieldType . IsClass )
8781 {
82+ Debug . Log ( property . JsonValue ) ;
8883 value = JsonUtility . FromJson ( property . JsonValue , fieldType ) ;
8984 return value != null ;
9085 }
9186
9287 Debug . LogError ( "Couldn't parse data" ) ;
9388 return false ;
9489 }
95-
96-
90+
91+
9792 /// <summary>
98- /// Gets the value in this property its type if possible (Only as wrapper).
93+ /// Gets the value in this property its type if possible (Only as wrapper).
9994 /// </summary>
10095 /// <param name="property">The notion property to read from.</param>
10196 /// <param name="fieldType">The field type to match.</param>
@@ -104,16 +99,16 @@ public static bool TryGetValueAs(NotionProperty property, Type fieldType, out ob
10499 public static bool TryGetValueAsWrapper ( NotionProperty property , Type fieldType , out object value )
105100 {
106101 if ( TryParseWrapper ( property , fieldType , out value ) ) return true ;
107-
102+
108103 Debug . LogError ( "Couldn't parse data" ) ;
109104 return false ;
110105 }
111106
112-
107+
113108 private static bool TryParseAsPrimitive ( NotionProperty property , Type fieldType , out object result )
114109 {
115110 result = null ;
116-
111+
117112 try
118113 {
119114 switch ( fieldType . Name )
@@ -137,15 +132,15 @@ private static bool TryParseAsPrimitive(NotionProperty property, Type fieldType,
137132
138133 return result != null ;
139134 }
140- #pragma warning disable 0168
135+ #pragma warning disable 0168
141136 catch ( Exception e )
142- #pragma warning restore
137+ #pragma warning restore
143138 {
144139 return result != null ;
145140 }
146141 }
147-
148-
142+
143+
149144 private static bool TryParseWrapper ( NotionProperty property , Type fieldType , out object result )
150145 {
151146 result = null ;
@@ -167,9 +162,9 @@ private static bool TryParseWrapper(NotionProperty property, Type fieldType, out
167162
168163 return result != null ;
169164 }
170- #pragma warning disable 0168
165+ #pragma warning disable 0168
171166 catch ( Exception e )
172- #pragma warning restore
167+ #pragma warning restore
173168 {
174169 return false ;
175170 }
@@ -190,9 +185,9 @@ private static JSONArray GetPropertyValueAsCollection(NotionProperty property)
190185 // Tries to parse as an array normally.
191186 return JSON . Parse ( property . JsonValue ) . AsArray ;
192187 }
193- #pragma warning disable 0168
188+ #pragma warning disable 0168
194189 catch ( Exception e )
195- #pragma warning restore
190+ #pragma warning restore
196191 {
197192 // Bracket the data in the [] so it can be parsed correctly.
198193 var builder = new StringBuilder ( ) ;
@@ -236,17 +231,17 @@ private static JSONArray GetPropertyValueAsCollection(NotionProperty property)
236231 }
237232 }
238233 }
239- #pragma warning disable 0168
234+ #pragma warning disable 0168
240235 catch ( Exception e )
241- #pragma warning restore
236+ #pragma warning restore
242237 {
243238 // If the value is already bracketed when downloading, just use that.
244239 //
245240 if ( property . JsonValue . Contains ( "[" ) && property . JsonValue . Contains ( "]" ) )
246241 {
247242 return JSON . Parse ( property . JsonValue ) . AsArray ;
248243 }
249-
244+
250245 //
251246 // Bracket the data in the [] so it can be parsed correctly.
252247 //
@@ -271,7 +266,6 @@ private static bool TryParseAsArray(NotionProperty property, Type fieldType, out
271266 try
272267 {
273268 var data = GetPropertyValueAsCollection ( property ) ;
274-
275269 if ( fieldType . GetElementType ( ) . IsEnum )
276270 {
277271 var parsedStringArray = new string [ data . Count ] ;
@@ -288,7 +282,7 @@ private static bool TryParseAsArray(NotionProperty property, Type fieldType, out
288282 {
289283 switch ( fieldType . GetElementType ( ) ? . Name )
290284 {
291- case { } x when x . Equals ( "Int" ) :
285+ case { } x when x . Contains ( "Int" ) :
292286
293287 var parsedIntArray = new int [ data . Count ] ;
294288
@@ -299,7 +293,7 @@ private static bool TryParseAsArray(NotionProperty property, Type fieldType, out
299293
300294 result = parsedIntArray ;
301295 break ;
302- case { } x when x . Equals ( "Boolean" ) :
296+ case { } x when x . Contains ( "Boolean" ) :
303297
304298 var parsedBoolArray = new bool [ data . Count ] ;
305299
@@ -310,7 +304,7 @@ private static bool TryParseAsArray(NotionProperty property, Type fieldType, out
310304
311305 result = parsedBoolArray ;
312306 break ;
313- case { } x when x . Equals ( "Single" ) :
307+ case { } x when x . Contains ( "Single" ) :
314308
315309 var parsedFloatArray = new float [ data . Count ] ;
316310
@@ -321,7 +315,7 @@ private static bool TryParseAsArray(NotionProperty property, Type fieldType, out
321315
322316 result = parsedFloatArray ;
323317 break ;
324- case { } x when x . Equals ( "Double" ) :
318+ case { } x when x . Contains ( "Double" ) :
325319
326320 var parsedDoubleArray = new double [ data . Count ] ;
327321
@@ -332,7 +326,7 @@ private static bool TryParseAsArray(NotionProperty property, Type fieldType, out
332326
333327 result = parsedDoubleArray ;
334328 break ;
335- case { } x when x . Equals ( "String" ) :
329+ case { } x when x . Contains ( "String" ) :
336330
337331 var parsedStringArray = new string [ data . Count ] ;
338332
@@ -350,9 +344,9 @@ private static bool TryParseAsArray(NotionProperty property, Type fieldType, out
350344
351345 return result != null ;
352346 }
353- #pragma warning disable 0168
347+ #pragma warning disable 0168
354348 catch ( Exception e )
355- #pragma warning restore
349+ #pragma warning restore
356350 {
357351 return false ;
358352 }
@@ -367,8 +361,8 @@ private static bool TryParseAsList(NotionProperty property, Type fieldType, out
367361 {
368362 var data = GetPropertyValueAsCollection ( property ) ;
369363 var typeName = fieldType . GenericTypeArguments . Length > 0
370- ? fieldType . GenericTypeArguments [ 0 ]
371- : fieldType ;
364+ ? fieldType . GenericTypeArguments [ 0 ]
365+ : fieldType ;
372366
373367
374368 switch ( typeName . Name )
@@ -434,9 +428,9 @@ private static bool TryParseAsList(NotionProperty property, Type fieldType, out
434428
435429 return result != null ;
436430 }
437- #pragma warning disable 0168
431+ #pragma warning disable 0168
438432 catch ( Exception e )
439- #pragma warning restore
433+ #pragma warning restore
440434 {
441435 return false ;
442436 }
@@ -459,15 +453,15 @@ private static bool TryParseEnumOrEnumFlags(NotionProperty property, Type fieldT
459453 result = Enum . Parse ( fieldType , elements [ 0 ] . Value . Replace ( " " , "" ) ) ;
460454 return result != null ;
461455 }
462-
456+
463457 for ( var index = 0 ; index < elements . Count ; index ++ )
464458 {
465459 combined += elements [ index ] . Value ;
466-
460+
467461 if ( index == elements . Count - 1 ) continue ;
468462 combined += "," ;
469463 }
470-
464+
471465 result = Enum . Parse ( fieldType , combined ) ;
472466 return result != null ;
473467 }
@@ -478,27 +472,27 @@ private static bool TryParseEnumOrEnumFlags(NotionProperty property, Type fieldT
478472 result = Enum . Parse ( fieldType , property . JsonValue . Replace ( " " , "" ) ) ;
479473 return result != null ;
480474 }
481- #pragma warning disable 0168
475+ #pragma warning disable 0168
482476 catch ( Exception e )
483- #pragma warning restore
477+ #pragma warning restore
484478 {
485479 result = fieldType . IsValueType ? Activator . CreateInstance ( fieldType ) : null ;
486480 return result != null ;
487481 }
488482 }
489483 }
490- #pragma warning disable 0168
484+ #pragma warning disable 0168
491485 catch ( Exception e )
492- #pragma warning restore
486+ #pragma warning restore
493487 {
494488 return false ;
495489 }
496490 }
497-
498-
491+
492+
499493 private static bool IsGenericList ( this Type o )
500494 {
501495 return ( o . IsGenericType && ( o . GetGenericTypeDefinition ( ) == typeof ( List < > ) ) ) ;
502496 }
503497 }
504- }
498+ }
0 commit comments