Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: integration with middleclass #64

Open
eliasdaler opened this issue Feb 27, 2023 · 1 comment
Open

Idea: integration with middleclass #64

eliasdaler opened this issue Feb 27, 2023 · 1 comment

Comments

@eliasdaler
Copy link

Hello.

I've been using middleclass and inspect for a long time, but today I came up with idea and want to hear your feedback :)

Currently, if you use a complex middleclass type with inspect, you get something like this:

{
  class = {
    __declaredMethods = {
      __tostring = <function 1>,
      getTile = <function 2>,
      initialize = <function 3>,
      isInstanceOf = <function 4>,
      loadFromJSON = <function 5>,
      setTile = <function 6>
    },
    __instanceDict = <1>{
      __index = <table 1>,
      __tostring = <function 1>,
      getTile = <function 2>,
      initialize = <function 3>,
      isInstanceOf = <function 4>,
      loadFromJSON = <function 5>,
      setTile = <function 6>
    },
    name = "TileMapLayer",
    static = <2>{
      allocate = <function 7>,
      include = <function 8>,
      isSubclassOf = <function 9>,
      new = <function 10>,
      subclass = <function 11>,
      subclassed = <function 12>,
      <metatable> = {
        __index = <function 13>
      }
    },
    subclasses = {
      <metatable> = {
        __mode = "k"
      }
    },
    <metatable> = {
      __call = <function 14>,
      __index = <table 2>,
      __newindex = <function 15>,
      __tostring = <function 16>
    }
  },
  tiles = {
    ["0,0"] = {
      class = <3>{
        __declaredMethods = {
          __tostring = <function 1>,
          initialize = <function 17>,
          isInstanceOf = <function 4>
        },
        __instanceDict = <4>{
          __index = <table 4>,
          __tostring = <function 1>,
          initialize = <function 17>,
          isInstanceOf = <function 4>
        },
        name = "Tile",
        static = <5>{
          allocate = <function 7>,
          include = <function 8>,
          isSubclassOf = <function 9>,
          new = <function 10>,
          subclass = <function 11>,
          subclassed = <function 12>,
          <metatable> = {
            __index = <function 18>
          }
        },
        subclasses = {
          <metatable> = {
            __mode = "k"
          }
        },
        <metatable> = {
          __call = <function 14>,
          __index = <table 5>,
          __newindex = <function 15>,
          __tostring = <function 16>
        }
      },
      id = 0,
      tid = 4,
      <metatable> = <table 4>
    },
    ...

This is very long and hard to read. However, with some edits to inspect.lua I was able to get this:

<TileMapLayer>{
  tiles = {
    ["0,0"] = <Tile>{
      id = 0,
      tid = 4
    },
    ["0,1"] = <Tile>{
      id = 1,
      tid = 4
    },
   ...
}

This is much easier to read and more useful for simple printf debugging. :)

Here's the diff:

@@ -163,7 +163,8 @@ local function getKeys(t)
 
    local keys, keysLen = {}, 0
    for k in rawpairs(t) do
-      if not isSequenceKey(k, seqLen) then
+      -- Elias Daler EDIT: don't print "class" table
+      if not isSequenceKey(k, seqLen) and k ~= "class" then
          keysLen = keysLen + 1
          keys[keysLen] = k
       end
@@ -273,6 +274,11 @@ function Inspector:putValue(v)
    elseif tv == 'table' and not self.ids[v] then
       local t = v
 
+      -- Elias Daler EDIT: print middleclass instance name
+      if type(t.class) == "table" then -- metaclass obj instatnce
+         puts(buf, "<" .. t.class.name .. ">")
+      end
+
       if t == inspect.KEY or t == inspect.METATABLE then
          puts(buf, tostring(t))
       elseif self.level >= self.depth then
@@ -305,13 +311,14 @@ function Inspector:putValue(v)
             end
          end
 
-         local mt = getmetatable(t)
+         -- Elias Daler EDIT: don't print metatables
+         --[[ local mt = getmetatable(t)
          if type(mt) == 'table' then
             if seqLen + keysLen > 0 then puts(buf, ',') end
             tabify(self)
             puts(buf, '<metatable> = ')
             self:putValue(mt)
-         end
+         end ]]--
 
          self.level = self.level - 1

Obviously, this functionality should be optional and configurable by options. But what do you think about it?
Maybe it's worth making some option like "middleclass_simple_print" or something?

@suy
Copy link

suy commented May 27, 2024

Maybe not exactly what you might need, but I have a middleclass-using object in which I add the following mixin:

    -- An "inspect" function on the instance that only prints its data.
    Inspectable = {
        inspect = function(self)
            local function only_inspect_data(item, path)
                if  path[#path] ~= inspect.METATABLE and
                    path[#path] ~= 'class' and
                    type(item) ~= 'function'
                then
                    return item
                end
            end

            return inspect(self, {process = only_inspect_data})
        end
    }

It's enough for my use case, as it just prints the values with data, which is exactly what I need in my case. It will do the same as 2 of your changes without having to patch inspect.lua. It would need extra work in order to do the printing of the class name in brackets, though, and I think that is not possible without changing the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants