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

Allow multiple root keys or nested data (not necessarily nested models) #21

Open
danvideo opened this issue Mar 27, 2017 · 1 comment
Open

Comments

@danvideo
Copy link

Maybe there's a better way to handle this, but right now it seems a root key that's multiple levels down is inaccessible. See this example json:

{
  "code": 0,
  "message": "success",
  "data": {
    "monitors": [{
      "name": "TYPE_ONE",
      "status": 1
    },{
      "name": "TYPE_TWO",
      "status": 1
    }],
    "monitor_groups": [{
      "status": 1,
      "monitors": [{
        "name": "DIFFERENT_TYPE",
        "status": 1
      }]
    }]
  }
}

For example if the values after doc["data"]["monitors"] are required (e.g. an array of TYPE_ONE and TYPE_TWO objects) this statement won't find the correct key
root_key plural: "monitors", singular: "monitors", scopes: [:read]
The ability to set another level of root keys such as the following would be great:
root_key_1 plural: "data", singular: "data", scopes: [:read]
root_key_2 plural: "monitors", singular: "monitor", scopes: [:read]
Alternatively to have the root key be able to start multiple levels into the json (although then there might be problems with similarly named keys)

@driv3r
Copy link

driv3r commented Mar 28, 2017

you can have a separate mapping class for monitors, and you can wrap the response within separate one depending on your business logic, i.e.

class Data
  attr_accessor :monitors, :monitor_groups
end

class Monitor
  attr_accessor :name, :status
end

class MonitorGroup
  attr_accessor :status, :monitors
end

class MonitorMapping
  include Kartograph::DSL
  
  kartograp do
    root_key singular: "monitor", plural: "monitors", scopes: [:read]
    mapping Monitor
    property "name", scopes: [:read]
    property "status", scopes: [:read]
  end
end

class ScreensMapping # ofc. you should pick better name ;)
  include Kartograph::DSL
  
  kartograp do
    root_key singular: "data", plural: "data", scopes: [:read]
    mapping Data
    property :monitors, plural: true, include: MonitorMapping
     
    property :monitor_groups, plural: true, scopes: [:read] do
      mapping MonitorGroup
      property :status, scopes: [:read]
      property :monitors, plural: true, scopes: [:read], include: MonitorMapping
    end
  end
end

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