{"id":16092337,"url":"https://github.com/alecthomas/prototemplate","last_synced_at":"2025-04-05T18:47:34.845Z","repository":{"id":27705450,"uuid":"31192331","full_name":"alecthomas/prototemplate","owner":"alecthomas","description":"Process Protocol Buffer definitions with text templates and JavaScript functions","archived":false,"fork":false,"pushed_at":"2024-01-01T08:15:38.000Z","size":38,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-18T17:07:49.642Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alecthomas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2015-02-23T03:19:44.000Z","updated_at":"2016-08-10T02:26:51.000Z","dependencies_parsed_at":"2024-10-31T10:22:49.328Z","dependency_job_id":"4f889f72-a92e-48a0-94ce-14b33e7ad586","html_url":"https://github.com/alecthomas/prototemplate","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/alecthomas%2Fprototemplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fprototemplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fprototemplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fprototemplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alecthomas","download_url":"https://codeload.github.com/alecthomas/prototemplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247385703,"owners_count":20930600,"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-10-09T16:07:08.769Z","updated_at":"2025-04-05T18:47:34.816Z","avatar_url":"https://github.com/alecthomas.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Process Protocol Buffer definitions with text templates and JavaScript functions\n\nThis tool is basically equivalent to any of the `protoc-gen-*` compilers\nexcept it is standalone, and uses JavaScript and [Go\ntemplates](https://github.com/alecthomas/template) to produce the generated\nfiles.\n\nIt works in the following way:\n\n1. The protocol buffer is compiled to a\n   [FileDescriptorSet](https://code.google.com/p/protobuf/source/browse/trunk/src/google/protobuf/descriptor.proto).\n2. CamelCase functions defined in the JavaScript file are exposed to the\n   template (same export rules as Go).\n3. The `FileDescriptorSet` is passed to the Go template as the top-level\n   context.\n\n## Why would you want to do this?\n\nIf you want to:\n\n- Generate custom RPC stubs from protobuf service definitions.\n- Generate documentation from your protobuf file.\n- ...\n\n\n## Installation\n\nFirst, install protoc. This varies by platform, but with Homebrew it's something like:\n\n```\n$ brew install protobuf\n```\n\nThen install prototemplate:\n\n```\n$ go get -ldflags \"-X main.TemplateDir ${GOPATH}/src/github.com/alecthomas/prototemplate/templates\" github.com/alecthomas/prototemplate\n```\n\nThe above incantation allows prototemplate to find its builtin templates.\n\n## Example\n\nThe following example will list all messages and fields in all files while stripping any leading package names present.\n\nProtocol buffer definition (test.proto):\n\n```proto\npackage test;\n\n// Defines what role a user has.\nenum Role {\n  EXECUTIVE = 1;\n  MANAGER = 2;\n  EMPLOYEE = 3;\n}\n\nmessage User {\n  required string name = 1;\n  optional int64 age = 2 [default=18];\n  repeated Role roles = 3;\n}\n\nmessage Group {\n  required User owner = 1;\n  repeated User users = 2;\n}\n```\n\nJavascript helper (test.js):\n\n```js\nfunction StripPackage(ref) {\n  return ref.replace(/^.*\\./, '');\n}\n```\n\nAnd this template (test.got):\n\n```\n{{with .FileDescriptorSet}}\\\n{{range .File}}\\\n{{range .MessageType}}\\\n{{.Name|StripPackage}}\n{{range .Field}}\\\n  {{.Name}} = {{.Number}}\n{{end}}\\\n{{end}}\\\n{{end}}\\\n{{end}}\\\n```\n\n*NOTE: prototemplate uses a fork of `text/template` that elides newlines when a closing delimiter is immediately followed by a `\\` (as seen above).*\n\nAnd invoke the utility like so:\n\n```\n$ prototemplate test.proto test.got test.js\nUser\n  name = 1\n  age = 2\n  roles = 3\nGroup\n  owner = 1\n  users = 2\n```\n\n## Usage\n\n```\n$ prototemplate --help\nusage: prototemplate [\u003cflags\u003e] \u003cproto\u003e \u003ctemplate\u003e [\u003cscript\u003e]\n\nFlags:\n  --help             Show help.\n  --templates=/Users/alec/.go/src/github.com/alecthomas/prototemplate/templates\n                     Root path to templates.\n  --list             List builtin generators.\n  --builtins         List builtin functions.\n  -I, --include=DIR  List of include paths to pass to protoc.\n  -o, --output=FILE  File to output generated template source to.\n\nArgs:\n  \u003cproto\u003e     Protocol buffer definition to compile.\n  \u003ctemplate\u003e  Template file, or name of a builtin generator.\n  [\u003cscript\u003e]  A JavaScript file defining template helper functions.\n```\n\n## Included templates\n\n### Swift\n\nThis generator currently includes a generator for [Swift](https://developer.apple.com/swift/), utilising the serialisation layer from the excellent [protobuf-swift](https://github.com/alexeyxo/protobuf-swift). This template has two goals:\n\n1. To work around [a bug](https://github.com/alexeyxo/protobuf-swift/issues/38) in the existing generated code.\n2. To generate more idiomatic code utilising Swift's optional types.\n\n\nUsage:\n\n```\n$ prototemplate test.proto swift\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fprototemplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falecthomas%2Fprototemplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fprototemplate/lists"}