Skip to content

Instantly share code, notes, and snippets.

@klokane
Last active August 29, 2015 14:23
Show Gist options
  • Save klokane/a09b33abb79fa5a14adb to your computer and use it in GitHub Desktop.
Save klokane/a09b33abb79fa5a14adb to your computer and use it in GitHub Desktop.
Refract element Mixins expansion
## Address (object)
- street
- city

## User (Address)
- id
- login

Nonexpanded version

[ "Address", { "id" : "User" }, {}, [
  [ "member", {}, {}, { "key" : "id" } ],
  [ "member", {}, {}, { "key" : "login" } ]
]

Copy && Expanded members solution

[ "object", { "id" : "User" }, {}, [
  [ "extend", { "ref" : "Address" }, {}, [
    [ "member", {}, {}, { "key" : "street" } ],
    [ "member", {}, {}, { "key" : "city" } ],
  ],
  [ "member", {}, {}, { "key" : "id" } ],
  [ "member", {}, {}, { "key" : "login" } ]
]

Members from parent comes first, to allow override them.

# A (object)
- a1
- include (B)
- a2

# B (C)
- b1

# C
- c1

Nonexpanded

[ "object", { "id" : "A" }, {}, [
  [ "member", {}, {}, { "key" : "a1" } ],
  [ "ref", {}, {}, 
   { 
      "href" : "B",
      "path" : "content"
    }
  ],
  [ "member", {}, {}, { "key" : "a2" } ]
]

Copy members

[ "object", { "id" : "A" }, {}, [
  [ "member", {}, {}, { "key" : "a1" } ],
  [ "extend", { "ref" : "B" }, {}, [
    [ "extend", { "ref" : "C" }, {}, [
      [ "member", {}, {}, { "key" : "c1" } ]
    ],
    [ "member", {}, {}, { "key" : "b1" } ],
  ],
  [ "member", {}, {}, { "key" : "a2" } ]
]

Problem

We need to solve expansion of MSON Mixins in refract spec.

Acceptable Criteria

It must preserve orders elements as they become from it original structures:

Input MSON

## Address (object)
- street
- city

## User (object)
- id
- Include Address
- login

Without expansion User MSON comes into refract:

[ "object", { "id" : "User" }, {}, [
  [ "member", {}, {}, { "key" : "id" } ],
  [ "ref", {}, {}, 
    { 
      "href" : "Address",
      "path" : ""content"
    }
  ],
  [ "member", {}, {}, { "key" : "login" } ]
]

As specified above we MUST preserve order of members in expanded element.

Copy members of referenced object into original object

[ "object", { "id" : "User" }, {}, [
  [ "member", {}, {}, { "key" : "id" } ],
  [ "extend", { "ref" : "Address" }, {}, [
    [ "member", {}, {}, { "key" : "street" } ],
    [ "member", {}, {}, { "key" : "city" } ],
  ],
  [ "member", {}, {}, { "key" : "login" } ]
]

This solution replace original referenced Element by extend and replace original element meta.id by meta.ref

@pksunkara
Copy link

I like the second solution.

There can be problem if referenced Mixin is not "ObjectElement"

Well, arrays can contain only array mixins. Objects can contain only object mixins. So, it shouldn't be a problem. But can you show an example where you think it might be?

@klokane
Copy link
Author

klokane commented Jun 24, 2015

About recursive version, there is question should member c1 reference

 [ "member", { "ref" : "C" }, {}, { "key" : "c1" } ],
  • C because original place
  • or B because it comes here from expansion

@klokane
Copy link
Author

klokane commented Jun 24, 2015

Another Question:

  • Are we able to decompose it to original structures?
  • It is required?

@klokane
Copy link
Author

klokane commented Jun 24, 2015

BTW, it seem

## User (Address)
- id
- login

Is equal definition to

## User (object)
- Include Address
- id
- login

@pksunkara
Copy link

@klokane, Can you add non-expanded version to recursive.md file? Thanks.

@pksunkara
Copy link

This is how arrays would look like:

References

## Hobbies (array)
- cricket
- gaming

## Interests (Hobbies)
- computers
- space

Non expanded

[ "Hobbies", { "id": "Interests" }, {}, [
  [ "string", {}, {}, "computers" ],
  [ "string", {}, {}, "space" ]
]]

Expanded

[ "array", { "id" : "Interests" }, {}, [
  [ "extend", { "ref" : "Hobbies" }, {}, [
    [ "string", {}, {}, "cricket" ],
    [ "string", {}, {}, "gaming" ],
  ],
  [ "string", {}, {}, "computers" ],
  [ "string", {}, {}, "space" ]
]]

Mixins

# A (array)
- a1
- include (B)
- a2

# B (C)
- b1

# C (array)
- c1

Expanded

[ "array", { "id" : "A" }, {}, [
  [ "string", {}, {}, "a1" ],
  [ "extend", { "ref" : "B" }, {}, [
    [ "extend", { "ref" : "C" }, {}, [
      [ "string", {}, {}, "c1" ]
    ],
    [ "string", {}, {}, "b1" ],
  ],
  [ "string", {}, {}, "a2" ]
]]

@klokane
Copy link
Author

klokane commented Jun 24, 2015

@pksunkara added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment