我试图使用PackageImports扩展来“隐藏”Scotty模块。
用例:使用库X的人的使instrumentedX可用。因此,只要在他们的阴谋文件中将X更改为instrumentedX,他们就可以使用该库的仪器化版本--而无需对代码进行任何其他更改。
下面是我想导出的模块,它具有相同的名称,即与Scotty导出的原始模块相同的Web.Scotty.Trans。
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PackageImports #-}
module Web.Scotty.Trans
(
module OS
, get
, post
, put
, delete
, patch
, options
, addroute
)
where
import qualified "scotty" Web.Scotty.Trans as OS hiding (get, post, put, delete, patch, options, addroute)
import qualified "scotty" Web.Scotty.Trans as S
import InstrumentedCore
import Control.Monad.Trans.Class(lift)
instrumentedAction original action = original action_
where
action_ = do
st <- liftIO $ getCurrentTime
result <- action
en <- liftIO $ getCurrentTime
logInstrumentationData InstrumentationData{instrStart=st, instrEnd=en, instrPayload=Render}
return result
get route action = instrumentedAction (S.get route) action
post route action = instrumentedAction (S.post route) action
put route action = instrumentedAction (S.put route) action
delete route action = instrumentedAction (S.delete route) action
patch route action = instrumentedAction (S.patch route) action
options route action = instrumentedAction (S.options route) action
addroute method route action = instrumentedAction (S.addroute method route) action我的阴谋集团文件是这样的。虽然我的包依赖于scotty,但它为自己使用了一个不同的包名,即instrumentedopaleye (不要进入语义--我也试图隐藏许多其他模块!)
name: instrumentedopaleye
version: 0.1.0.0
synopsis: Initial project template from stack
description: Please see README.md
homepage: https://github.com/githubuser/dashboard#readme
license: BSD3
author: Author name here
maintainer: example@example.com
copyright: 2016 Author name here
category: Web
build-type: Simple
extra-source-files: README.md
cabal-version: >=1.10
library
hs-source-dirs: src
exposed-modules: InstrumentedOpaleye
, Instrumented
, InstrumentedOpaleye.Internal.PGTypes
, InstrumentedOpaleye.Internal.Column
, InstrumentedOpaleye.Internal.RunQuery
, InstrumentedOpaleye.Internal.HaskellDB.PrimQuery
, InstrumentedLucid
, Web.Scotty.Trans
, InstrumentedCore
build-depends: base >= 4.7 && < 5
, opaleye
, profunctors
, product-profunctors
, postgresql-simple
, mtl
, time
, stm
, text
, uuid
, lucid
, scotty
, transformers
default-language: Haskell2010实际错误
下面是当我试图在Web.Scotty.Trans中导入我的版本的GHCi时所发生的事情。考虑到我只显式导入了,我的Web.Scotty.Trans版本为什么GHCi在解析get函数时感到困惑?就连:browse Web.Scotty.Trans也开始困惑了。
Saurabhs-MacBook-Pro:instrumentedopaleye saurabhnanda$ stack exec ghci
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
Prelude> :set -fobject-code
Prelude> :set -XPackageImports
Prelude> import "instrumentedopaleye" Web.Scotty.Trans
Prelude Web.Scotty.Trans> :i get
<interactive>:1:1: error:
Ambiguous interface for ‘Web.Scotty.Trans’:
it was found in multiple packages:
scotty-0.11.0 instrumentedopaleye-0.1.0.0
Prelude Web.Scotty.Trans> :browse Web.Scotty.Trans
<no location info>: error:
Ambiguous module name ‘Web.Scotty.Trans’:
it was found in multiple packages:
scotty-0.11.0@scotty-0.11.0-2sNTuGP3mb3FV66hlreoEd instrumentedopaleye-0.1.0.0@instrumentedopaleye-0.1.0.0-EAOrgrhUGi83yaJJ8gDGX4发布于 2017-01-27 17:49:33
甚至在您做包合格导入之前,我就看到了这种行为。例如,如果以前已经安装了cryptonite和crypto-api,那么
> stack exec ghci
$ :browse Crypto.Random导致ambiguous module name错误。这是我经常遇到的问题;我的解决方案是使用-hide-package标志:
> stack exec ghci
$ :set -hide-package cryptonite
$ :browse Crypto.Random另外,您根本不需要PackageImports:ghci完全忽略了cryptonite。
发布于 2017-01-28 05:00:26
我无法在GHCi中完成这一工作,除非按照crockeea的回答显式地隐藏scotty
此外,由于Haskell的模块限制,不能再导出一个合格的进口。因此,下面是我如何解决我最初的跟踪scotty的问题:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PackageImports #-}
module Web.Scotty.Trans
(
module Web.Scotty.Trans -- which is the current module
, module Web.Scotty.TransOriginal -- the original module MINUS the shadowed methods
)
where
import Web.Scotty.TransOriginal
import qualified "scotty" Web.Scotty.Trans as S
import InstrumentedCore
import Control.Monad.Trans.Class(lift)
instrumentedAction original action = original action_
where
action_ = do
st <- liftIO $ getCurrentTime
result <- action
en <- liftIO $ getCurrentTime
logInstrumentationData InstrumentationData{instrStart=st, instrEnd=en, instrPayload=Render}
return result
get route action = instrumentedAction (S.get route) action
post route action = instrumentedAction (S.post route) action
put route action = instrumentedAction (S.put route) action
delete route action = instrumentedAction (S.delete route) action
patch route action = instrumentedAction (S.patch route) action
options route action = instrumentedAction (S.options route) action
addroute method route action = instrumentedAction (S.addroute method route) action样板Web.Scotty.TransOriginal只需要绕过Haskell对合格进口产品的再出口限制:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PackageImports #-}
module Web.Scotty.TransOriginal
(
module Web.Scotty.Trans
)
where
import "scotty" Web.Scotty.Trans hiding (get, post, put, delete, patch, options, addroute)https://stackoverflow.com/questions/41896211
复制相似问题