-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCoreDataHs.hs
73 lines (58 loc) · 2.24 KB
/
CoreDataHs.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{-# LANGUAGE OverloadedStrings #-}
module CoreDataHs
( modelEntities
, fullModelPath
, versionModelPath
, findEntity
, Entity(Entity)
, Attribute(Attribute, entName, attrType)
, entityName
, entityAttributes
, attrName
) where
import Text.XML.Light
import Data.List
import Data.Text(Text, pack)
import Data.Monoid
data Attribute = Attribute {
attrName :: Text
, attrType :: Text
, entName :: Text
} deriving (Show)
data Entity = Entity {
entityName :: Text
, entityAttributes :: [Attribute]
, entityRelationships :: [Text]
} deriving (Show)
simpleName :: String -> QName
simpleName s = QName s Nothing Nothing
typeAttr :: Element -> Text
typeAttr e = case (findAttr $ simpleName "attributeType") e of
Just a -> pack a
Nothing -> error "element 'attributeType' not found"
nameAttr :: Element -> Text
nameAttr e = case (findAttr $ simpleName "name") e of
Just a -> pack a
Nothing -> error "element 'name' not found"
attrElements :: Element -> [Element]
attrElements = findChildren $ simpleName "attribute"
relChild :: Element -> [Element]
relChild = findChildren $ simpleName "relationship"
relationships :: Element -> [Text]
relationships e = map nameAttr (relChild e)
buildEntity :: Element -> Entity
buildEntity e = Entity (nameAttr e) [buildAttribute x e | x <- attrElements e] (relationships e)
entityAttrs :: Element -> [Text]
entityAttrs e = map nameAttr (attrElements e)
findEntity :: String -> [Entity] -> Maybe Entity
findEntity "" _ = Nothing
findEntity _ [] = Nothing
findEntity s e = find (\(Entity name _ _) -> name == pack s) e
buildAttribute :: Element -> Element -> Attribute
buildAttribute e b = Attribute (nameAttr e) (typeAttr e) (nameAttr b)
fullModelPath :: String -> String
fullModelPath s = s <> ".xcdatamodeld/" <> s <> ".xcdatamodel/contents"
versionModelPath :: String -> String -> String
versionModelPath s v = s <> ".xcdatamodeld/" <> s <> "_" <> v <> ".xcdatamodel/contents"
modelEntities :: Text -> [Entity]
modelEntities = map buildEntity . concatMap (findElements $ simpleName "entity") . onlyElems . parseXML