{"id":28441514,"url":"https://github.com/sangria-graphql/macro-visit","last_synced_at":"2025-10-24T22:33:56.162Z","repository":{"id":38711320,"uuid":"84016726","full_name":"sangria-graphql/macro-visit","owner":"sangria-graphql","description":"A macro-based generic visitor generator","archived":false,"fork":false,"pushed_at":"2025-02-24T10:58:43.000Z","size":428,"stargazers_count":24,"open_issues_count":11,"forks_count":13,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-06-06T04:41:39.725Z","etag":null,"topics":["ast","case-class","macros","scala","visitor"],"latest_commit_sha":null,"homepage":"http://sangria-graphql.org","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sangria-graphql.png","metadata":{"files":{"readme":"README.md","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":"2017-03-06T01:29:34.000Z","updated_at":"2024-12-02T10:27:49.000Z","dependencies_parsed_at":"2023-02-14T15:46:45.110Z","dependency_job_id":"b43021d6-15b1-4f65-a7bb-f1787243acbd","html_url":"https://github.com/sangria-graphql/macro-visit","commit_stats":{"total_commits":197,"total_committers":12,"mean_commits":"16.416666666666668","dds":0.5482233502538071,"last_synced_commit":"abe05029a260f42737d0766710ef89d92af744a1"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/sangria-graphql/macro-visit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangria-graphql%2Fmacro-visit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangria-graphql%2Fmacro-visit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangria-graphql%2Fmacro-visit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangria-graphql%2Fmacro-visit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sangria-graphql","download_url":"https://codeload.github.com/sangria-graphql/macro-visit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangria-graphql%2Fmacro-visit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261539319,"owners_count":23174136,"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":["ast","case-class","macros","scala","visitor"],"created_at":"2025-06-06T04:39:46.185Z","updated_at":"2025-10-24T22:33:51.124Z","avatar_url":"https://github.com/sangria-graphql.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Macro Visit \n\nA macro-based generic visitor generator\n\n![Continuous Integration](https://github.com/sangria-graphql/macro-visit/workflows/Continuous%20Integration/badge.svg)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.sangria-graphql/macro-visit_2.13/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.sangria-graphql/macro-visit_2.13)\n[![License](http://img.shields.io/:license-Apache%202-brightgreen.svg)](http://www.apache.org/licenses/LICENSE-2.0.txt)\n[![Join the chat at https://gitter.im/sangria-graphql/sangria](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/sangria-graphql/sangria?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nSBT Configuration:\n\n```scala\nlibraryDependencies += \"org.sangria-graphql\" %% \"macro-visit\" % \"\u003clatest version\u003e\"\n```\n\n## Introduction\n\nWriting visitor code can be quite tedious, especially if some nodes need to be transformed (in immutable way) along the way. This becomes even harder if performance is a concern and data structures are deeply recursive, so non tail-recursive approach is not an option. **macro-visit** provides very simple way to create type-safe visitor code for arbitrary sealed case class hierarchies. Generated visitor provides following features:\n\n* **Non-recursive traversal**, which means all state is managed in the heap and you will not run into stack \n  overflow errors with deep recursive data structures. \n* **Optimised for performance and memory footprint**. Generated code for class hierarchy traversal is compiled into tight `while` loop.\n* **Allows to transform** traversed object in immutable way. It generates code that uses case class's `copy` method to get updated \n  instance of object in most efficient way (if object has several changes, it would be copied only once)\n* **Allows to break traversal at any given node and skip nodes**\n* **Supports `List`, `Vector`, `Seq` and `Option` traversal**\n  \nGenerated visitors can be very useful for traversing and transforming AST (Abstract Syntax Tree).        \n\nAssuming that the base type is called `Ast`, following visitors are supported:\n\n* `Visit(enter, leave)`, where `enter` and `leave` are `Ast =\u003e VisitorCommand` functions - it recursively visits all instances of `Ast` type\n* `VisitAnyField(fn)` - it visits all fields with special type `S` (which is different from `Ast`) along the way \n* `VisitAnyFieldByName(fieldName, fn)` - it visits all fields with provided name and special type `S` (which is different from `Ast`) along the way\n \nFollowing visitor commands are supported:\n \n* `Skip`\n* `Continue`\n* `Break`\n* `Transform(newValue, controlCommand)`\n* `Delete`\n* `DeleteAndBreak` \n\n## Example\n\nHere is how basic usage looks like:\n\n```scala\nimport sangria.visitor._\n\nval res = visit[Ast](root,\n  Visit[Field](f =\u003e if (f.name == \"hex\") VisitorCommand.Delete else VisitorCommand.Continue),\n  Visit[IntValue](v =\u003e VisitorCommand.Transform(IntValue(v.value + 1))))\n```\n\nMacro will look for all subtypes of `Ast` and then will generate traversal code for it with provided visitors. `Visit` takes 2 functions \nas an argument `enter` and `leave` which both have `Ast ⇒ VisitorCommand` type.\n \nGiven following AST definitions:\n \n```scala\nsealed trait Ast\n\ncase class Vertex(name: String, edges: List[Edge], fields: Vector[Field] = Vector.empty) extends Ast\ncase class Edge(weight: Int, toVertex: Vertex) extends Ast\ncase class Field(name: String, value: Option[Value]) extends Ast\n\nsealed trait Value extends Ast\n\ncase class StringValue(value: String) extends Value\ncase class IntValue(value: Int) extends Value\n``` \n\nand sample data:\n\n```scala\nval root =\n  Vertex(\"start\", List(\n    Edge(1, Vertex(\"colors\", List(\n      Edge(2, Vertex(\"RED\", Nil, Vector(\n        Field(\"intensity\", Some(IntValue(123))),\n        Field(\"hex\", Some(StringValue(\"#FF0000\")))\n      ))),\n      Edge(100, Vertex(\"GREEN\", Nil, Vector(\n        Field(\"hex\", Some(StringValue(\"#00FF00\")))\n      )))\n    ))),\n    Edge(42, Vertex(\"books\", List(\n      Edge(1, Vertex(\"The Hobbit\", Nil, Vector(\n        Field(\"pages\", Some(IntValue(320)))\n      )))\n    )))\n  ))\n```\n\nThe result of the transformation will look like this:\n\n```scala\nVertex(\"start\", List(\n  Edge(1, Vertex(\"colors\", List(\n    Edge(2,Vertex(\"RED\", Nil, Vector(\n      Field(\"intensity\", Some(IntValue(124)))))),\n    Edge(100, Vertex(\"GREEN\", Nil, Vector.empty))), Vector.empty)),\n  Edge(42, Vertex(\"books\", List(\n    Edge(1, Vertex(\"The Hobbit\", Nil, Vector(\n      Field(\"pages\", Some(IntValue(321))))))),\n    Vector.empty))),\n  Vector.empty)\n```\n\n## License\n\n**macro-visit** is licensed under [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsangria-graphql%2Fmacro-visit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsangria-graphql%2Fmacro-visit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsangria-graphql%2Fmacro-visit/lists"}