{"id":19326626,"url":"https://github.com/thought-machine/go-protoparser","last_synced_at":"2025-02-24T06:21:09.779Z","repository":{"id":57491209,"uuid":"198233326","full_name":"thought-machine/go-protoparser","owner":"thought-machine","description":"Yet another Go package which parses a Protocol Buffer file (proto3)","archived":false,"fork":false,"pushed_at":"2020-04-09T11:48:40.000Z","size":276,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":27,"default_branch":"master","last_synced_at":"2025-01-06T07:12:11.136Z","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/thought-machine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-22T13:53:52.000Z","updated_at":"2022-04-12T15:14:23.000Z","dependencies_parsed_at":"2022-08-30T04:00:24.741Z","dependency_job_id":null,"html_url":"https://github.com/thought-machine/go-protoparser","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thought-machine%2Fgo-protoparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thought-machine%2Fgo-protoparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thought-machine%2Fgo-protoparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thought-machine%2Fgo-protoparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thought-machine","download_url":"https://codeload.github.com/thought-machine/go-protoparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240427460,"owners_count":19799498,"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-10T02:14:21.197Z","updated_at":"2025-02-24T06:21:09.746Z","avatar_url":"https://github.com/thought-machine.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-protoparser [![GoDoc](https://godoc.org/github.com/yoheimuta/go-protoparser?status.svg)](https://godoc.org/github.com/yoheimuta/go-protoparser)[![CircleCI](https://circleci.com/gh/yoheimuta/go-protoparser/tree/master.svg?style=svg)](https://circleci.com/gh/yoheimuta/go-protoparser/tree/master)[![Go Report Card](https://goreportcard.com/badge/github.com/yoheimuta/go-protoparser)](https://goreportcard.com/report/github.com/yoheimuta/go-protoparser)[![Release](http://img.shields.io/github/release/yoheimuta/go-protoparser.svg?style=flat)](https://github.com/yoheimuta/go-protoparser/releases/latest)[![License](http://img.shields.io/:license-mit-blue.svg)](https://github.com/yoheimuta/go-protoparser/blob/master/LICENSE.md)\n\ngo-protoparser is a yet another Go package which parses a Protocol Buffer file (proto3).\n\n- Conforms to the exactly [official spec](https://developers.google.com/protocol-buffers/docs/reference/proto3-spec).\n- Undergone rigorous testing. The parser can parses all examples of the official spec well.\n- Easy to use the parser. You can just call the [Parse function](https://godoc.org/github.com/yoheimuta/go-protoparser#Parse) and receive the [Proto struct](https://godoc.org/github.com/yoheimuta/go-protoparser/parser#Proto).\n  - If you don't care about the order of body elements, consider to use the [unordered.Proto struct](https://godoc.org/github.com/yoheimuta/go-protoparser/interpret/unordered#Proto).\n  - Or if you want to use the visitor pattern, use the [Visitor struct](https://godoc.org/github.com/yoheimuta/go-protoparser/parser#Visitor).\n\n### Installation\n\n```\ngo get github.com/yoheimuta/go-protoparser\n```\n\n### Example\n\nA Protocol Buffer file versioned 3 which is [an example of the official reference](https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#proto_file).\n\n```proto\nsyntax = \"proto3\";\n// An example of the official reference\n// See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#proto_file\npackage examplepb;\nimport public \"other.proto\";\noption java_package = \"com.example.foo\";\nenum EnumAllowingAlias {\n    option allow_alias = true;\n    UNKNOWN = 0;\n    STARTED = 1;\n    RUNNING = 2 [(custom_option) = \"hello world\"];\n}\nmessage outer {\n    option (my_option).a = true;\n    message inner {   // Level 2\n        int64 ival = 1;\n    }\n    repeated inner inner_message = 2;\n    EnumAllowingAlias enum_field =3;\n    map\u003cint32, string\u003e my_map = 4;\n}\n```\n\nThe Parsed result is a Go typed struct. The below output is encoded to JSON for simplicity.\n\n```json\n{\n  \"Syntax\": {\n    \"ProtobufVersion\": \"proto3\",\n    \"Comments\": null,\n    \"InlineComment\": null,\n    \"Meta\": {\n      \"Pos\": {\n        \"Filename\": \"simple.proto\",\n        \"Offset\": 0,\n        \"Line\": 1,\n        \"Column\": 1\n      }\n    }\n  },\n  \"ProtoBody\": [\n    {\n      \"Name\": \"examplepb\",\n      \"Comments\": [\n        {\n          \"Raw\": \"// An example of the official reference\",\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 19,\n              \"Line\": 2,\n              \"Column\": 1\n            }\n          }\n        },\n        {\n          \"Raw\": \"// See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#proto_file\",\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 59,\n              \"Line\": 3,\n              \"Column\": 1\n            }\n          }\n        }\n      ],\n      \"InlineComment\": null,\n      \"Meta\": {\n        \"Pos\": {\n          \"Filename\": \"simple.proto\",\n          \"Offset\": 151,\n          \"Line\": 4,\n          \"Column\": 1\n        }\n      }\n    },\n    {\n      \"Modifier\": 1,\n      \"Location\": \"\\\"other.proto\\\"\",\n      \"Comments\": null,\n      \"InlineComment\": null,\n      \"Meta\": {\n        \"Pos\": {\n          \"Filename\": \"simple.proto\",\n          \"Offset\": 170,\n          \"Line\": 5,\n          \"Column\": 1\n        }\n      }\n    },\n    {\n      \"OptionName\": \"java_package\",\n      \"Constant\": \"\\\"com.example.foo\\\"\",\n      \"Comments\": null,\n      \"InlineComment\": null,\n      \"Meta\": {\n        \"Pos\": {\n          \"Filename\": \"simple.proto\",\n          \"Offset\": 199,\n          \"Line\": 6,\n          \"Column\": 1\n        }\n      }\n    },\n    {\n      \"EnumName\": \"EnumAllowingAlias\",\n      \"EnumBody\": [\n        {\n          \"OptionName\": \"allow_alias\",\n          \"Constant\": \"true\",\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 269,\n              \"Line\": 8,\n              \"Column\": 5\n            }\n          }\n        },\n        {\n          \"Ident\": \"UNKNOWN\",\n          \"Number\": \"0\",\n          \"EnumValueOptions\": null,\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 300,\n              \"Line\": 9,\n              \"Column\": 5\n            }\n          }\n        },\n        {\n          \"Ident\": \"STARTED\",\n          \"Number\": \"1\",\n          \"EnumValueOptions\": null,\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 317,\n              \"Line\": 10,\n              \"Column\": 5\n            }\n          }\n        },\n        {\n          \"Ident\": \"RUNNING\",\n          \"Number\": \"2\",\n          \"EnumValueOptions\": [\n            {\n              \"OptionName\": \"(custom_option)\",\n              \"Constant\": \"\\\"hello world\\\"\"\n            }\n          ],\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 334,\n              \"Line\": 11,\n              \"Column\": 5\n            }\n          }\n        }\n      ],\n      \"Comments\": null,\n      \"InlineComment\": null,\n      \"InlineCommentBehindLeftCurly\": null,\n      \"Meta\": {\n        \"Pos\": {\n          \"Filename\": \"simple.proto\",\n          \"Offset\": 240,\n          \"Line\": 7,\n          \"Column\": 1\n        }\n      }\n    },\n    {\n      \"MessageName\": \"outer\",\n      \"MessageBody\": [\n        {\n          \"OptionName\": \"(my_option).a\",\n          \"Constant\": \"true\",\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 403,\n              \"Line\": 14,\n              \"Column\": 5\n            }\n          }\n        },\n        {\n          \"MessageName\": \"inner\",\n          \"MessageBody\": [\n            {\n              \"IsRepeated\": false,\n              \"Type\": \"int64\",\n              \"FieldName\": \"ival\",\n              \"FieldNumber\": \"1\",\n              \"FieldOptions\": null,\n              \"Comments\": null,\n              \"InlineComment\": null,\n              \"Meta\": {\n                \"Pos\": {\n                  \"Filename\": \"simple.proto\",\n                  \"Offset\": 471,\n                  \"Line\": 16,\n                  \"Column\": 7\n                }\n              }\n            }\n          ],\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"InlineCommentBehindLeftCurly\": {\n            \"Raw\": \"// Level 2\",\n            \"Meta\": {\n              \"Pos\": {\n                \"Filename\": \"simple.proto\",\n                \"Offset\": 454,\n                \"Line\": 15,\n                \"Column\": 23\n              }\n            }\n          },\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 436,\n              \"Line\": 15,\n              \"Column\": 5\n            }\n          }\n        },\n        {\n          \"IsRepeated\": true,\n          \"Type\": \"inner\",\n          \"FieldName\": \"inner_message\",\n          \"FieldNumber\": \"2\",\n          \"FieldOptions\": null,\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 497,\n              \"Line\": 18,\n              \"Column\": 5\n            }\n          }\n        },\n        {\n          \"IsRepeated\": false,\n          \"Type\": \"EnumAllowingAlias\",\n          \"FieldName\": \"enum_field\",\n          \"FieldNumber\": \"3\",\n          \"FieldOptions\": null,\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 535,\n              \"Line\": 19,\n              \"Column\": 5\n            }\n          }\n        },\n        {\n          \"KeyType\": \"int32\",\n          \"Type\": \"string\",\n          \"MapName\": \"my_map\",\n          \"FieldNumber\": \"4\",\n          \"FieldOptions\": null,\n          \"Comments\": null,\n          \"InlineComment\": null,\n          \"Meta\": {\n            \"Pos\": {\n              \"Filename\": \"simple.proto\",\n              \"Offset\": 572,\n              \"Line\": 20,\n              \"Column\": 5\n            }\n          }\n        }\n      ],\n      \"Comments\": null,\n      \"InlineComment\": null,\n      \"InlineCommentBehindLeftCurly\": null,\n      \"Meta\": {\n        \"Pos\": {\n          \"Filename\": \"simple.proto\",\n          \"Offset\": 383,\n          \"Line\": 13,\n          \"Column\": 1\n        }\n      }\n    }\n  ],\n  \"Meta\": {\n    \"Filename\": \"simple.proto\"\n  }\n}\n```\n\n### Usage\n\nSee also `_example/dump`.\n\n```go\nfunc run() int {\n\tflag.Parse()\n\n\treader, err := os.Open(*proto)\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"failed to open %s, err %v\\n\", *proto, err)\n\t\treturn 1\n\t}\n\tdefer reader.Close()\n\n\tgot, err := protoparser.Parse(reader)\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"failed to parse, err %v\\n\", err)\n\t\treturn 1\n\t}\n\n\tgotJSON, err := json.MarshalIndent(got, \"\", \"  \")\n\tif err != nil {\n\t\tfmt.Fprintf(os.Stderr, \"failed to marshal, err %v\\n\", err)\n\t}\n\tfmt.Print(string(gotJSON))\n\treturn 0\n}\n\nfunc main() {\n\tos.Exit(run())\n}\n```\n\n### Users\n\n- [protolint](https://github.com/yoheimuta/protolint)\n\n### Motivation\n\nThere exists the similar protobuf parser packages in Go.\n\nBut I could not find the parser which just return a parsing result well-typed value.\nA parser which supports a visitor pattern is useful to implement like linter, but it may be difficult to use.\nIt can be sufficient for most parsing situations to just return a parsing result well-typed value.\nThis is easier to use.\n\n### Acknowledgement\n\nThank you to the proto package: https://github.com/emicklei/proto\n\nI referred to the package for the good proven design, interface and some source code.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthought-machine%2Fgo-protoparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthought-machine%2Fgo-protoparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthought-machine%2Fgo-protoparser/lists"}