{"id":21597264,"url":"https://github.com/spheresoftware/go-annotations","last_synced_at":"2025-03-18T11:22:13.016Z","repository":{"id":138507648,"uuid":"71145485","full_name":"SphereSoftware/go-annotations","owner":"SphereSoftware","description":"Go Annotations","archived":false,"fork":false,"pushed_at":"2016-10-19T18:37:53.000Z","size":16,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-24T17:36:43.533Z","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/SphereSoftware.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2016-10-17T14:12:16.000Z","updated_at":"2024-10-24T05:05:06.000Z","dependencies_parsed_at":"2023-03-16T19:15:25.766Z","dependency_job_id":null,"html_url":"https://github.com/SphereSoftware/go-annotations","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SphereSoftware%2Fgo-annotations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SphereSoftware%2Fgo-annotations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SphereSoftware%2Fgo-annotations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SphereSoftware%2Fgo-annotations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SphereSoftware","download_url":"https://codeload.github.com/SphereSoftware/go-annotations/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244208608,"owners_count":20416110,"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":"2024-11-24T18:08:35.334Z","updated_at":"2025-03-18T11:22:13.010Z","avatar_url":"https://github.com/SphereSoftware.png","language":"Go","readme":"# Go Annotations\n\nAnnotations support for Go\n\n\nThis project provides ability to add Java-style annotations in the comments to struct, interface, method and/or func.\nAnnotations from comments are transformed into objects and mapped to provided struct for use in the code.\nCode generation (go generate) is used to create the registry of all annotations in the project.\n\n## Quick start:\n\n1. Make sure you're using Go 1.6+\n2. Install or update it:  `go get -u github.com/SphereSoftware/go-annotations`\n3. Define you first annotation in any go source file, for example `annotation.go`:\n\n   ```go\n   type Entity struct {\n       Name string `default:\"unknown\"`\n   }\n   ```\n\n4. Define your annotated entity in any other (or the same) go source file `example.go`:\n\n   ```go\n   package example\n\n   //go:generate go-annotations\n\n   type (\n      // @Entity(name=\"test\")\n      Person struct {\n         Id       int\n         FullName string\n      }\n   )\n   ```\n\n5. Use that annotation in the code somewhere else:\n\n   ```go\n   import (\n     \"github.com/SphereSoftware/go-annotations/registry\"\n   )\n\n   func Test() {\n       a := registry.GetStructAnnotatoin(Person{}).(Entity)\n       ...\n   }\n   ```\n\n6. Run `go generate [package or file]`. This will create `example_annotations.go`\n   in the same package with type `Person`\n\n## Features\n\n* Annotation class inside the comments is started with the '@' character\n* Top-level annotation's package should be included with _ alias\n* Structures, interfaces, methods and functions can be annotated\n* Annotation can contain parameters: comma-separated list of property-value pairs\n* If annotation has only one attribute then only its value can be specified as the parameter\n* Array property value is enclosed by {} and elements are comma-separated\n* Property value can be string, number or another annotation\n* For each annotation the corresponding struct should be defined\n* Optional annotation attributes are defined as pointers types\n* Defauld attribute value could be specified using field tag \"default:\"\n* Annotation registry file is generated for the whole package\n* At least one source with annotations should contain `go:generate` tag\n* The optional parameter of `go:generate` tag can specify the name of generated source file for regstry\n* Default registry source is named `\u003cpackage_name\u003e`+\"_annotations.go\"\n* The set of methods is provided to get annotations list for specified struct, func, interface or field name\n\n## API\n\nThe following functions from `registry` package are provided to work with annotations:\n\n* `func Map(s string, a Annotations)` - associates the set of annotations with given tag. Tag contains the\ninformation about full package name and object name in form of `\u003cfull_package_name\u003e`.`\u003cobject_name\u003e`\n* `func MapType(i interface{}, a Annotations)` - maps annotation bundle to the type and name of provided object\n* `func GetStructAnnotations(s interface{}) []interface{}` - returns struct/interface/func level annotations.\nParameter can be either object instance or object's `refect.Type` instance or string with object's package and\nname information, as described for `Map` function\n* `func GetFieldAnnotations(s interface{}, fieldName string) []interface{}` - returns annotations bundle for \nspecified field of provided object type\n* `func GetMethodAnnotations(s interface{}, methodName string) []interface{}` - returns annotations bundle for \nspecified method of provided object type\n* `func GetFuncAnnotation(s interface{}) []interface{}` - returns annotations bundle for provided func type\n\n## More examples of annotations\n\n   ```go\n   package example\n\n   import (\n       \"github.com/SphereSoftware/go-annotations/example/test\"\n       \"github.com/SphereSoftware/go-annotations/registry\"\n   )\n\n   //go:generate go-annotations my_annotations\n\n   type (\n       // @Entity(Name=\"test\", Books={@Book(Name=\"book\",Author=@Person(\"Mr.X\"))})\n       Test struct{}\n      \n      //@Entity\n      Test2 struct {\n          //@Book\n          Name string\n      }\n\n      // @Entity\n      Sample interface {\n          // @Book\n          doSomething(s string) int\n      }\n   )\n\n   // @Entity\n   func (*Test) methodOfTest() {\n       ...\n   }\n\n   // @Book\n   func JustAFunc() {\n       ...\n   }\n   ```\n\n   ```go\n   package test\n\n   import (\n       r \"github.com/SphereSoftware/go-annotations/example/test2\"\n   )\n\n   type (\n       Entity struct {\n           Name  string\n           Books []Book\n       }\n\n       Book struct {\n           Name   string\n           Price  float32 `default:\"1.0\"`\n           Author *r.Person\n       }\n   )\n   ```\n\n   ```go\n   package test2\n\n   type (\n       Person struct {\n           Name string\n       }\n   )\n   ```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspheresoftware%2Fgo-annotations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspheresoftware%2Fgo-annotations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspheresoftware%2Fgo-annotations/lists"}