From: Alexandre Terrasa Date: Thu, 19 Oct 2017 00:27:19 +0000 (-0400) Subject: model_json: update ModelView X-Git-Url: http://nitlanguage.org model_json: update ModelView Signed-off-by: Alexandre Terrasa --- diff --git a/src/model/model_json.nit b/src/model/model_json.nit index 7c43e51..bf7e6a6 100644 --- a/src/model/model_json.nit +++ b/src/model/model_json.nit @@ -57,9 +57,9 @@ redef class MEntity # Serialize the full version of `self` to JSON # # See: `FullJsonSerializer` - fun serialize_to_full_json(plain, pretty: nullable Bool): String do + fun serialize_to_full_json(mainmodule: MModule, plain, pretty: nullable Bool): String do var stream = new StringWriter - var serializer = new FullJsonSerializer(stream) + var serializer = new FullJsonSerializer(stream, mainmodule) serializer.plain_json = plain or else false serializer.pretty_json = pretty or else false serializer.serialize self @@ -72,10 +72,14 @@ redef class MEntity # By default, every reference to another MEntity is replaced by a pointer # to the MEntity::json_id. # Use this method to obtain a full object with mentities instead of pointers. - fun to_full_json: String do return serialize_to_full_json(plain=true) + fun to_full_json(mainmodule: MModule): String do + return serialize_to_full_json(mainmodule, plain=true) + end # Same as `to_full_json` but with pretty json. - fun to_pretty_full_json: String do return serialize_to_full_json(plain=true, pretty=true) + fun to_pretty_full_json(mainmodule: MModule): String do + return serialize_to_full_json(mainmodule, plain=true, pretty=true) + end # Sort mentities by name private fun sort_entities(mentities: Collection[MEntity]): Array[MEntity] do @@ -141,7 +145,7 @@ redef class MModule redef fun core_serialize_to(v) do super if v isa FullJsonSerializer then - var view = private_view + var view = new ModelView(model, v.mainmodule) v.serialize_attribute("mpackage", to_mentity_ref(mpackage)) v.serialize_attribute("mgroup", to_mentity_ref(mgroup)) v.serialize_attribute("intro_mclasses", to_mentity_refs(sort_entities(intro_mclasses))) @@ -158,7 +162,7 @@ redef class MClass super v.serialize_attribute("mparameters", mparameters) if v isa FullJsonSerializer then - var view = private_view + var view = new ModelView(model, v.mainmodule) v.serialize_attribute("intro", to_mentity_ref(intro)) v.serialize_attribute("intro_mmodule", to_mentity_ref(intro_mmodule)) v.serialize_attribute("mpackage", to_mentity_ref(intro_mmodule.mpackage)) @@ -177,7 +181,7 @@ redef class MClassDef v.serialize_attribute("is_intro", is_intro) v.serialize_attribute("mparameters", mclass.mparameters) if v isa FullJsonSerializer then - var view = private_view + var view = new ModelView(model, v.mainmodule) v.serialize_attribute("mmodule", to_mentity_ref(mmodule)) v.serialize_attribute("mclass", to_mentity_ref(mclass)) v.serialize_attribute("mpropdefs", to_mentity_refs(sort_entities(mpropdefs))) @@ -311,4 +315,7 @@ end # See MEntity::to_full_json. class FullJsonSerializer super JsonSerializer + + # FIXME tmp use of the mainmodule, a PR is comming to clean all the JSON mess + var mainmodule: MModule end diff --git a/src/model/test_model_json.nit b/src/model/test_model_json.nit index c544446..cc10f48 100644 --- a/src/model/test_model_json.nit +++ b/src/model/test_model_json.nit @@ -23,6 +23,8 @@ class TestModelSerialization var suite_path: String = "NIT_TESTING_PATH".environ var lib_path: String = "{suite_path.dirname}/../../tests/test_prog" + var mainmodule: MModule is noinit + private var model: Model do var toolcontext = new ToolContext var model = new Model @@ -31,6 +33,7 @@ class TestModelSerialization if mmodules.is_empty then return model mbuilder.run_phases toolcontext.run_global_phases(mmodules) + mainmodule = mmodules.first return model end @@ -40,54 +43,54 @@ class TestModelSerialization mentities.add model.mmodules.first mentities.add model.mclasses.first for mentity in mentities do - print ((new MEntityRef(mentity)).to_pretty_full_json) + print ((new MEntityRef(mentity)).to_pretty_full_json(mainmodule)) end end fun test_packages_to_full_json is test do for mentity in model.mpackages do - print mentity.to_pretty_full_json + print mentity.to_pretty_full_json(mainmodule) end end fun test_groups_to_full_json is test do for mpackage in model.mpackages do for mentity in mpackage.mgroups do - print mentity.to_pretty_full_json + print mentity.to_pretty_full_json(mainmodule) end end end fun test_modules_to_full_json is test do for mentity in model.mmodules do - print mentity.to_pretty_full_json + print mentity.to_pretty_full_json(mainmodule) end end fun test_classes_to_full_json is test do for mentity in model.mclasses do - print mentity.to_pretty_full_json + print mentity.to_pretty_full_json(mainmodule) end end fun test_classdefs_to_full_json is test do for mclass in model.mclasses do for mentity in mclass.mclassdefs do - print mentity.to_pretty_full_json + print mentity.to_pretty_full_json(mainmodule) end end end fun test_props_to_full_json is test do for mentity in model.mproperties do - print mentity.to_pretty_full_json + print mentity.to_pretty_full_json(mainmodule) end end fun test_propdefs_to_full_json is test do for mprop in model.mproperties do for mentity in mprop.mpropdefs do - print mentity.to_pretty_full_json + print mentity.to_pretty_full_json(mainmodule) end end end diff --git a/src/model/test_model_json.sav/test_classes_to_full_json.res b/src/model/test_model_json.sav/test_classes_to_full_json.res index d5eb364..a267d33 100644 --- a/src/model/test_model_json.sav/test_classes_to_full_json.res +++ b/src/model/test_model_json.sav/test_classes_to_full_json.res @@ -1133,8 +1133,6 @@ }], "parents": [{ "full_name": "test_prog::Combatable" - }, { - "full_name": "test_prog::Object" }] } { diff --git a/src/web/api_model.nit b/src/web/api_model.nit index 3d09a83..8dafca9 100644 --- a/src/web/api_model.nit +++ b/src/web/api_model.nit @@ -150,7 +150,7 @@ class APIEntity redef fun get(req, res) do var mentity = mentity_from_uri(req, res) if mentity == null then return - res.raw_json mentity.to_full_json + res.raw_json mentity.to_full_json(config.view.mainmodule) end end