{"id":24995937,"url":"https://github.com/fclairamb/restruct","last_synced_at":"2025-03-29T15:16:12.391Z","repository":{"id":59046884,"uuid":"533498881","full_name":"fclairamb/restruct","owner":"fclairamb","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-23T13:42:15.000Z","size":3007,"stargazers_count":1,"open_issues_count":6,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-23T22:53:24.956Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","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/fclairamb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","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":"2022-09-06T20:53:34.000Z","updated_at":"2023-12-06T15:30:39.000Z","dependencies_parsed_at":"2024-08-28T03:22:03.047Z","dependency_job_id":"967abb01-377b-4abc-b229-fb4a8ec155fa","html_url":"https://github.com/fclairamb/restruct","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fclairamb%2Frestruct","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fclairamb%2Frestruct/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fclairamb%2Frestruct/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fclairamb%2Frestruct/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fclairamb","download_url":"https://codeload.github.com/fclairamb/restruct/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246200324,"owners_count":20739567,"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":[],"created_at":"2025-02-04T15:53:02.245Z","updated_at":"2025-03-29T15:16:12.339Z","avatar_url":"https://github.com/fclairamb.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Regex to struct library\n\n[![Go version](https://img.shields.io/github/go-mod/go-version/fclairamb/restruct)](https://golang.org/doc/devel/release.html)\n[![Release](https://img.shields.io/github/v/release/fclairamb/restruct)](https://github.com/fclairamb/restruct/releases/latest)\n[![Build](https://github.com/fclairamb/restruct/workflows/Build/badge.svg)](https://github.com/fclairamb/restruct/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/fclairamb/restruct/branch/main/graph/badge.svg?token=y1vcrxbXfv)](https://codecov.io/gh/fclairamb/restruct)\u003c!--- [![gocover.io](https://gocover.io/_badge/github.com/fclairamb/restruct)](https://gocover.io/github.com/fclairamb/restruct) --\u003e\n[![Go Report Card](https://goreportcard.com/badge/fclairamb/restruct)](https://goreportcard.com/report/fclairamb/restruct)\n[![GoDoc](https://godoc.org/github.com/fclairamb/restruct?status.svg)](https://godoc.org/github.com/fclairamb/restruct)\n\n## General idea\nThis is a very simple library that allows you to convert a regex into a struct. It's intended to be used for simple text parsing around \ndummy bots.\n\nThe struct shall have a field for each capture group of the regex.\n\n## Usage\n\nThis can be tested [on the playground](https://go.dev/play/p/beFzEua9vlE).\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\n\tr \"github.com/fclairamb/restruct\"\n)\n\nfunc main() {\n\n\ttype Human struct {\n\t\tName   string `restruct:\"name\"` // Specifying the field\n\t\tAge    int    // No tag, \"age\" will be used\n\t\tHeight *int   // A pointer will be set to nil if the capture group is empty\n\t}\n\n\trs := \u0026r.Restruct{\n\t\tRegexToStructs: []*r.RegexToStruct{\n\t\t\t{\n\t\t\t\tID:     \"age\",\n\t\t\t\tRegex:  `^(?P\u003cname\u003e\\w+) is (?P\u003cage\u003e\\d+)( years old)?$`,\n\t\t\t\tStruct: \u0026Human{},\n\t\t\t},\n\t\t\t{\n\t\t\t\tID:     \"height\",\n\t\t\t\tRegex:  `^(?P\u003cname\u003e\\w+) is (?P\u003cheight\u003e\\d+) cm tall$`,\n\t\t\t\tStruct: \u0026Human{},\n\t\t\t},\n\t\t},\n\t}\n\n\tfor _, input := range []string{\"John is 178 cm tall\", \"John is 42 years old\"} {\n\t\tfmt.Println(\"input:\", input)\n\t\tm, _ := rs.MatchString(input)\n\n\t\tif m == nil {\n\t\t\tfmt.Printf(`No match for \"%s\"`, input)\n\n\t\t\tcontinue\n\t\t}\n\n\t\tfmt.Println(\"  match ID:\", m.ID)\n\n\t\th := m.Struct.(*Human)\n\t\tfmt.Printf(\"  name = %v, age = %v\", h.Name, h.Age)\n\n\t\tif h.Height != nil {\n\t\t\tfmt.Printf(\", height = %v\", *h.Height)\n\t\t}\n\n\t\tfmt.Printf(\"\\n\")\n\t}\n}\n```\nThis will produce:\n```\ninput: John is 178 cm tall\n  match ID: height\n  name = John, age = 0, height = 178\ninput: John is 42 years old\n  match ID: age\n  name = John, age = 42\n```\n\n## Benchmark\nIt's _not_ fast:\n```text\ngo test -bench=. -benchmem\ngoos: darwin\ngoarch: arm64\npkg: github.com/fclairamb/restruct/test\nBenchmarkSmallStruct\nBenchmarkSmallStruct-8    \t 2813796\t       406.5 ns/op\t     145 B/op\t       6 allocs/op\nBenchmarkThreeRules-8     \t 1869470\t       651.4 ns/op\t     145 B/op\t       6 allocs/op\nBenchmarkBiggerStruct-8   \t 2122315\t       564.2 ns/op\t     177 B/op\t      10 allocs/op\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffclairamb%2Frestruct","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffclairamb%2Frestruct","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffclairamb%2Frestruct/lists"}