{"id":16001656,"url":"https://github.com/pbrisbin/yaml-marked","last_synced_at":"2025-08-18T10:32:34.400Z","repository":{"id":212668553,"uuid":"732036187","full_name":"pbrisbin/yaml-marked","owner":"pbrisbin","description":"Data.Yaml with Marks","archived":false,"fork":false,"pushed_at":"2024-11-25T20:12:15.000Z","size":123,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-25T20:39:59.602Z","etag":null,"topics":["haskell","yaml"],"latest_commit_sha":null,"homepage":"","language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pbrisbin.png","metadata":{"files":{"readme":"README.lhs","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-15T13:34:51.000Z","updated_at":"2024-11-25T20:12:06.000Z","dependencies_parsed_at":"2024-07-31T23:34:16.678Z","dependency_job_id":null,"html_url":"https://github.com/pbrisbin/yaml-marked","commit_stats":null,"previous_names":["pbrisbin/yaml-marked"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrisbin%2Fyaml-marked","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrisbin%2Fyaml-marked/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrisbin%2Fyaml-marked/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pbrisbin%2Fyaml-marked/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pbrisbin","download_url":"https://codeload.github.com/pbrisbin/yaml-marked/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230226393,"owners_count":18193157,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["haskell","yaml"],"created_at":"2024-10-08T09:45:29.208Z","updated_at":"2024-12-18T06:22:02.270Z","avatar_url":"https://github.com/pbrisbin.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"## yaml-marked\n\n[![Hackage](https://img.shields.io/hackage/v/yaml-marked.svg?style=flat)](https://hackage.haskell.org/package/yaml-marked)\n[![Stackage Nightly](http://stackage.org/package/yaml-marked/badge/nightly)](http://stackage.org/nightly/package/yaml-marked)\n[![Stackage LTS](http://stackage.org/package/yaml-marked/badge/lts)](http://stackage.org/lts/package/yaml-marked)\n[![CI](https://github.com/pbrisbin/yaml-marked/actions/workflows/ci.yml/badge.svg)](https://github.com/pbrisbin/yaml-marked/actions/workflows/ci.yml)\n\nLike `Data.Yaml` but for when you need the source locations (aka `YamlMark`s)\nfor parsed elements.\n\n## Motivation\n\nWhile working on [`stack-lint-extra-deps`][sled], it became apparent it would be\nimpossible to implement an auto-fix in a way that preserved the source\nformatting of the original Yaml. Sure, we could decode, modify, and encode, but\nthat loses all formatting, comments, and sorting -- which can be important for\ndocumenting choices made in one's `stack.yaml`.\n\n[sled]: https://github.com/freckle/stack-lint-extra-deps\n\nThat is, unless there were a Yaml parser that preserved source locations. Then,\nI could use those to make targeted replacements within the original source\n`ByteString`, while preserving everything else.\n\nThere didn't seem to be anything promising on Hackage, except for the fact that\n`Text.Libyaml` (the library powering `Data.Yaml`) has functions that emit\n`MarkedEvent`s, which `Data.Yaml` doesn't use.\n\nSo I began the process of modifying `Data.Yaml` internals to operate on\n`MarkedEvent`s and, instead of assuming `FromJSON` to construct a `Value`, use a\nprovided decoding function and produce my own `Marked Value` type -- which is\nbasically the same, except holding the original `YamlMark` (recursively). Thus,\n`Data.Yaml.Marked.Decode` was born.\n\nFinally, I created something to approximate a non-typeclass `FromJSON`\n(`Data.Yaml.Marked.Parse`), and a module to handle the tricky business of\napplying replacements to `ByteString`s (`Data.Yaml.Marked.Replace`), and I can\nfinally do what I need to:\n\n## Example\n\n\u003c!--\n```haskell\n{-# OPTIONS_GHC -Wno-unused-top-binds #-}\nmodule Main (main) where\nimport Prelude\nimport Text.Markdown.Unlit ()\n```\n--\u003e\n\n### Imports\n\n```haskell\nimport Data.Text (Text)\nimport qualified Data.ByteString.Char8 as BS8\nimport Data.Yaml.Marked\nimport Data.Yaml.Marked.Value\nimport Data.Yaml.Marked.Parse\nimport Data.Yaml.Marked.Decode\nimport Data.Yaml.Marked.Replace\n```\n\n### Decoding\n\nDecoding is meant to look a lot like aeson. Your record should hold `Marked`\nvalues anywhere you will need that:\n\n```haskell\ndata StackYaml = StackYaml\n  { resolver :: Marked Text\n  , extraDeps :: Marked [Marked Text]\n  }\n```\n\nInstead of making a `FromJSON` instance, you just define a function. The\n`Data.Yaml.Marked.Parse` module exposes combinators to accomplish this:\n\n```haskell\ndecodeStackYaml :: Marked Value -\u003e Either String (Marked StackYaml)\ndecodeStackYaml = withObject \"StackYaml\" $ \\o -\u003e\n  StackYaml\n    \u003c$\u003e (text =\u003c\u003c (o .: \"resolver\"))\n    \u003c*\u003e (array text =\u003c\u003c (o .: \"extra-deps\"))\n```\n\nThen we can give this and some source Yaml to\n`Data.Yaml.Marked.Decode.decodeThrow` and get back a `Marked StackYaml`:\n\n```haskell\nexample :: IO ()\nexample = do\n  stackYaml \u003c- BS8.readFile \"stack.yaml\"\n  -- Imagine:\n  --\n  --   resolver: lts-20.0\n  --   extra-deps:\n  --    - ../local-package\n  --    - hackage-dep-1.0\n  --\n\n  StackYaml {..} \u003c-\n    markedItem \u003c$\u003e decodeThrow decodeStackYaml \"stack.yaml\" stackYaml\n```\n\nBecause our decoder returns a `Marked StackYaml`, that's what we get. We don't\nneed the location info for this (it just represents the entire file), so we\ndiscard it here. We could've written our decoder to return just a `StackYaml`,\nbut then we could not have used `withObject`, which always returns `Marked` in\ncase it's being used somewhere other than the top-level document, where you\nwould indeed want `Marked` values.\n\nNext, let's pretend we linted this value and discovered the resolver and the\nsecond extra-dep can be updated. As part of doing this, we would presumably\nbuild `Replace` values from the `Marked` items we linted:\n\n```haskell\n  let replaces =\n        [ replaceMarked resolver \"lts-20.11\"\n        , replaceMarked (markedItem extraDeps !! 1) \"hackage-dep-2.0.1\"\n        ]\n```\n\nThen we can runs all of those on the original `ByteString`:\n\n```haskell\n  BS8.putStr =\u003c\u003c runReplaces replaces stackYaml\n  --\n  -- Outputs:\n  --\n  --   resolver: lts-20.11\n  --   extra-deps:\n  --    - ../local-package\n  --    - hackage-dep-2.0.1\n  --\n```\n\n\u003c!--\n```haskell\nmain :: IO ()\nmain = pure ()\n```\n---\u003e\n\n## Development \u0026 Tests\n\n```console\nstack build --fast --pedantic --test --file-watch\n```\n\n---\n\n[LICENSE](./LICENSE) | [CHANGELOG](./CHANGELOG.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpbrisbin%2Fyaml-marked","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpbrisbin%2Fyaml-marked","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpbrisbin%2Fyaml-marked/lists"}