serialization: move up type name manipulation services out of `json`
authorAlexis Laferrière <alexis.laf@xymus.net>
Wed, 26 Jul 2017 12:05:02 +0000 (08:05 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 31 Aug 2017 17:23:57 +0000 (13:23 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/json/serialization_read.nit
lib/serialization/engine_tools.nit
lib/serialization/serialization.nit

index 13a48d1..5c259ff 100644 (file)
@@ -180,10 +180,7 @@ class JsonDeserializer
 
                                        if class_name == null and static_type != null then
                                                # Fallack to the static type, strip the `nullable` prefix
-                                               var prefix = "nullable "
-                                               if static_type.has_prefix(prefix) then
-                                                       class_name = static_type.substring_from(prefix.length)
-                                               else class_name = static_type
+                                               class_name = static_type.strip_nullable
                                        end
                                end
 
@@ -203,8 +200,8 @@ class JsonDeserializer
                                end
 
                                if static_type != null and check_subtypes then
-                                       var static_class = static_type.strip_nullable_and_params
-                                       var dynamic_class = class_name.strip_nullable_and_params
+                                       var static_class = static_type.strip_nullable_and_params.to_s
+                                       var dynamic_class = class_name.strip_nullable_and_params.to_s
                                        if not class_inheritance_metamodel.has_edge(dynamic_class, static_class) then
                                                errors.add new Error("Deserialization Error: `{class_name}` is not a subtype of the static type `{static_type}`")
                                                return null
@@ -260,13 +257,8 @@ class JsonDeserializer
                if object isa Array[nullable Object] then
                        # Can we use the static type?
                        if static_type != null then
-                               var prefix = "nullable "
-                               var class_name = if static_type.has(prefix) then
-                                               static_type.substring_from(prefix.length)
-                                       else static_type
-
                                opened_array = object
-                               var value = deserialize_class(class_name)
+                               var value = deserialize_class(static_type.strip_nullable)
                                opened_array = null
                                return value
                        end
@@ -431,25 +423,6 @@ redef class Text
                end
                return res
        end
-
-       # Strip the `nullable` prefix and the params from the class name `self`
-       #
-       # ~~~nitish
-       # assert "String".strip_nullable_and_params == "String"
-       # assert "Array[Int]".strip_nullable_and_params == "Array"
-       # assert "Map[Set[String], Set[Int]]".strip_nullable_and_params == "Map"
-       # ~~~
-       private fun strip_nullable_and_params: String
-       do
-               var class_name = to_s
-
-               var prefix = "nullable "
-               if class_name.has_prefix(prefix) then class_name = class_name.substring_from(prefix.length)
-
-               var bracket_index = class_name.index_of('[')
-               if bracket_index == -1 then return class_name
-               return class_name.substring(0, bracket_index)
-       end
 end
 
 redef class SimpleCollection[E]
index 4742ad4..f085749 100644 (file)
@@ -44,3 +44,35 @@ class StrictHashMap[K, V]
                return c
        end
 end
+
+redef class Text
+
+       # Strip the `nullable` prefix from the type name `self`
+       #
+       # ~~~
+       # assert "String".strip_nullable == "String"
+       # assert "nullable Array[Int]".strip_nullable == "Array[Int]"
+       # assert "Map[Set[String], Set[Int]]".strip_nullable == "Map[Set[String], Set[Int]]"
+       # ~~~
+       fun strip_nullable: Text
+       do
+               var prefix = "nullable "
+               return if has_prefix(prefix) then substring_from(prefix.length) else self
+       end
+
+       # Strip the `nullable` prefix and the params from the type name `self`
+       #
+       # ~~~
+       # assert "String".strip_nullable_and_params == "String"
+       # assert "nullable Array[Int]".strip_nullable_and_params == "Array"
+       # assert "Map[Set[String], Set[Int]]".strip_nullable_and_params == "Map"
+       # ~~~
+       fun strip_nullable_and_params: Text
+       do
+               var class_name = strip_nullable
+
+               var bracket_index = class_name.index_of('[')
+               if bracket_index == -1 then return class_name
+               return class_name.substring(0, bracket_index)
+       end
+end
index fa97009..3147cad 100644 (file)
@@ -141,7 +141,7 @@ abstract class Deserializer
        # This method should be redefined for each custom subclass of `Serializable`.
        # All refinement should look for a precise `class_name` and call super
        # on unsupported classes.
-       protected fun deserialize_class(class_name: String): nullable Object do
+       protected fun deserialize_class(class_name: Text): nullable Object do
                if class_name == "Error" then return new Error.from_deserializer(self)
                return deserialize_class_intern(class_name)
        end
@@ -151,7 +151,7 @@ abstract class Deserializer
        # Refinements to this method will be generated by the serialization phase.
        # To avoid conflicts, there should not be any other refinements to this method.
        # You can instead use `deserialize_class`.
-       protected fun deserialize_class_intern(class_name: String): nullable Object do
+       protected fun deserialize_class_intern(class_name: Text): nullable Object do
                errors.add new Error("Deserialization Error: Doesn't know how to deserialize class \"{class_name}\"")
                return null
        end