在 Persistent 中使用生成的镜头从字段中删除下划线
Remove underscore from fields with generated lenses in Persistent
假设我有一个持久类型并希望从该类型中投射一些值:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
name Text
email Text
|]
...
getName :: Entity User -> Text
getName (Entity uid vals) = userName vals
问题是,如果我为所述类型生成镜头,使用 mkPersist sqlSettings {mpsGenerateLenses = True}
,我需要在每个投影函数的开头添加下划线或使用镜头 getter:
getName :: Entity User -> Text
getName (Entity uid vals) = _userName vals
getName' :: Entity User -> Text
getName (Entity uid vals) = vals ^. userName
- 首先,如何将其恢复为默认值
userName vals
,并添加下划线以使用镜头 getter、vals ^. _userName
?
- 其次,为什么是这样而不是相反?
Firstly, how can I revert that to the default, userName vals
, and add the underscore to use the lenses getter, vals ^. _userName
?
Database.Persist.TH
does not offer that option (to see what it might look like if it existed, cf. Control.Lens.TH
), so, assuming that you won't fork the library over this, there doesn't seem to be a way. (By the way, looking for mpsGenerateLenses
in the source 将准确显示添加下划线的位置。)
Secondly, why is this this way and not the other way around?
大概是因为库假设如果您生成镜头,您将在任何地方使用它们而不是记录 accessors/labels,包括获取字段的值。我唯一的美化建议是,如果将书写顺序从 _userName vals
更改为 vals ^. userName
困扰您,您可能更喜欢使用 view
而不是 (^.)
,如 view userName vals
.
假设我有一个持久类型并希望从该类型中投射一些值:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
name Text
email Text
|]
...
getName :: Entity User -> Text
getName (Entity uid vals) = userName vals
问题是,如果我为所述类型生成镜头,使用 mkPersist sqlSettings {mpsGenerateLenses = True}
,我需要在每个投影函数的开头添加下划线或使用镜头 getter:
getName :: Entity User -> Text
getName (Entity uid vals) = _userName vals
getName' :: Entity User -> Text
getName (Entity uid vals) = vals ^. userName
- 首先,如何将其恢复为默认值
userName vals
,并添加下划线以使用镜头 getter、vals ^. _userName
? - 其次,为什么是这样而不是相反?
Firstly, how can I revert that to the default,
userName vals
, and add the underscore to use the lenses getter,vals ^. _userName
?
Database.Persist.TH
does not offer that option (to see what it might look like if it existed, cf. Control.Lens.TH
), so, assuming that you won't fork the library over this, there doesn't seem to be a way. (By the way, looking for mpsGenerateLenses
in the source 将准确显示添加下划线的位置。)
Secondly, why is this this way and not the other way around?
大概是因为库假设如果您生成镜头,您将在任何地方使用它们而不是记录 accessors/labels,包括获取字段的值。我唯一的美化建议是,如果将书写顺序从 _userName vals
更改为 vals ^. userName
困扰您,您可能更喜欢使用 view
而不是 (^.)
,如 view userName vals
.