Skip to content
Browse files

Rename fields upon splicing (fix the group fields name clash issue)

  • Loading branch information...
1 parent 87b9c1c commit eca16ddffa6b161247e4d7a7e4b50d2b4b548736 @dzhus committed Mar 22, 2012
Showing with 35 additions and 7 deletions.
  1. +21 −4 README.org
  2. +14 −3 src/Snap/Snaplet/Redson/Snapless/Metamodel.hs
View
25 README.org
@@ -319,10 +319,27 @@
`meta` for every field as served as-is without any changes.
- Every field with type `group` is spliced into list of actual group
- members in served model, and `groupName` property is attached to
- every field in splice result. Client may use this data to
- recognize fields from the same group and render them specifically.
+ Every field `f` with type `group` is spliced into list of actual
+ group members in served model, and `groupName` property is
+ attached to every field in splice result, with value equal to name
+ of group. Name of every field in group is prepended with `f:`,
+ where `f` is the name of original field which was spliced into
+ group. Client may use this data to recognize fields from the same
+ group and render them specifically.
+
+ /Example/:
+
+ Assuming group `bar` has fields named `f1`, `f2` and `f3`, and
+ model has field with group splice annotation:
+
+ #+BEGIN_SRC javascript
+ "name": "foo",
+ "groupName": "bar"
+ #+END_SRC
+
+ then `foo` will be spliced into fields named `foo:f1`, `foo:f2`
+ and `foo:f3`, and `groupName` for all these fields will be set to
+ `bar`.
Client may also request list of readable models from
View
17 src/Snap/Snaplet/Redson/Snapless/Metamodel.hs
@@ -146,6 +146,15 @@ instance ToJSON Field where
type Groups = M.Map B.ByteString [Field]
+-- | Build new name `f:gK` for every field of group `g` to which field
+-- `f` is spliced into.
+groupFieldName :: FieldName
+ -- ^ Name of field which is spliced into group
+ -> FieldName
+ -- ^ Name of group field
+ -> FieldName
+groupFieldName parent field = B.concat [parent, ":", field]
+
-- | Replace all model fields having `group` type with actual group
-- fields.
spliceGroups :: Groups -> Model -> Model
@@ -155,11 +164,13 @@ spliceGroups groups model =
in
model{fields = concat $
map (\f ->
- case (groupName f, fieldType f) of
- (Just n, "group") ->
+ case groupName f of
+ Just n ->
case (M.lookup n groups) of
Just grp ->
- map (\gf -> gf{groupName = Just n}) grp
+ map (\gf -> gf{ groupName = Just n
+ , name = groupFieldName (name f) (name gf)
+ }) grp
Nothing -> [f]
_ -> [f]
) origFields}

0 comments on commit eca16dd

Please sign in to comment.
Something went wrong with that request. Please try again.