json – 如何应对Haskell名称空间?

前端之家收集整理的这篇文章主要介绍了json – 如何应对Haskell名称空间?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个数据记录来编组JIRA JSON对象.问题是,多个对象具有相同的名称/值对标签.例如 :

(从curl返回并格式化)

{"expand":"schema,names","startAt":0,"maxResults":2,"total":74,"issues":[
           {"expand":"editMeta,renderedFields,transitions,changelog,operations","id":"183614","self":"https://10.64.16.44/rest/api/latest/issue/183614","key":"BNAP-339","fields":{"versions":[
                                  {"self":"https://10.64.16.44/rest/api/2/version/28240","id":"28240","name":"2012-12-07","archived":false,"released":false
                                  }
                                 ],"status":{"self":"https://10.64.16.44/rest/api/2/status/1","description":"The issue is open and ready for the assignee to start work on it.","iconUrl":"https://10.64.16.44/images/icons/status_open.gif","name":"Open","id":"1"
                               },"description":"Do Re Mi Fa","resolution":null
                     }
           }
          ]

当我构造有问题的相应Haskell数据记录时,我得到:

data Issue = Issue {expand :: String,id :: String,self :: String,key :: String,fields :: Fields
                   } deriving Generic


data Version = Version {self :: String,name :: String,archived :: Bool,released :: Bool
                       } deriving Generic

‘id’和’self’会发生冲突.它发生在我身上我可以通过更改记录中的名称并使用手动创建的FromJSON实例来修复它.
欢迎任何替代解决方案.

解决方法

我通过将问题和版本之类的东西放在同一层次结构中的单独文件中来解决这个问题.

Haskell只使用单独的模块来控制名称空间,因此这是正统的解决方案.

非常漂亮:使用类型类来定义可用名称

class Has'self a b | a -> bwhere
   get'self :: a -> b
   set'self :: b -> a -> b

instance Has'self Issue String where ...
instance Has'self Version String where ....

编辑:下面的评论提醒我提供更详细的建议.不要使用Has’self喜欢的解决方案 – 那些已经走过那条道路报告它变得难看.我可以保证单独模块的路径.

PS:也许你可以在你的领域使用lens库!

猜你在找的JavaScript相关文章