Skip to content

Instantly share code, notes, and snippets.

@madjar
Created May 23, 2019 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madjar/35311de77a70a3a833fca2b252c0e438 to your computer and use it in GitHub Desktop.
Save madjar/35311de77a70a3a833fca2b252c0e438 to your computer and use it in GitHub Desktop.
#!/usr/bin/env stack
-- stack --resolver lts-13.15 script
{-# LANGUAGE OverloadedStrings, NoImplicitPrelude #-}
import Language.Haskell.Interpreter
import Options.Applicative.Simple
import Protolude hiding (to)
import Prelude (String)
import Control.Lens hiding (set)
import Data.Aeson
import Data.Aeson.Lens
main = do
((file, lensArg, edit), ()) <-
simpleOptions
"json-edit"
"Edit a json file based on lenses"
""
((,,) <$> strOption (short 'f' <> metavar "FILE") <*> strArgument (metavar "LENS") <*> switch (short 'e' <> long "--edit"))
empty
fileContent <- readFile file
if edit
then return ()
else do
lens <- fmap runTraversal (interpretLens lensArg)
liftIO . print $ (fileContent ^.. lens)
interpretLens :: String -> IO (ReifiedTraversal' Text Text)
interpretLens s = do
result <- runInterpreter $ do
setImports ["Data.Aeson", "Data.Aeson.Lens", "Control.Lens", "Control.Lens.Internal.Prism", "Data.Text", "Prelude"]
interpret ("Traversal "<> (parens s)) infer
case result of
Right prism -> return prism
Left e@(WontCompile errors) -> traverse_ (putStrLn . errMsg) errors >> exitFailure
Left e -> print e >> exitFailure
@madjar
Copy link
Author

madjar commented May 23, 2019

This is a (failed) attempt at making a tool that allows you to zoom into part of a json file (or any lens for that matter) with your editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment