{"id":26235615,"url":"https://github.com/onexstack/protoc-gen-defaults","last_synced_at":"2026-04-13T05:41:43.466Z","repository":{"id":271093968,"uuid":"908650582","full_name":"onexstack/protoc-gen-defaults","owner":"onexstack","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-05T12:41:00.000Z","size":76,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-01-15T07:10:59.819Z","etag":null,"topics":["ai","cloudnative","docker","go","golang","kubernetes"],"latest_commit_sha":null,"homepage":"https://konglingfei.com/","language":"Go","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/onexstack.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":"2024-12-26T15:47:59.000Z","updated_at":"2025-04-24T15:02:34.000Z","dependencies_parsed_at":"2025-01-05T13:27:26.588Z","dependency_job_id":"38f9088b-19d0-454e-824f-54dfc7061151","html_url":"https://github.com/onexstack/protoc-gen-defaults","commit_stats":null,"previous_names":["onexstack/protoc-gen-defaults","superproj/protoc-gen-defaults"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/onexstack/protoc-gen-defaults","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onexstack%2Fprotoc-gen-defaults","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onexstack%2Fprotoc-gen-defaults/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onexstack%2Fprotoc-gen-defaults/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onexstack%2Fprotoc-gen-defaults/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/onexstack","download_url":"https://codeload.github.com/onexstack/protoc-gen-defaults/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/onexstack%2Fprotoc-gen-defaults/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31741541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-13T05:13:27.074Z","status":"ssl_error","status_checked_at":"2026-04-13T05:13:25.150Z","response_time":93,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["ai","cloudnative","docker","go","golang","kubernetes"],"created_at":"2025-03-13T03:16:46.128Z","updated_at":"2026-04-13T05:41:43.432Z","avatar_url":"https://github.com/onexstack.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# protoc-gen-defaults\n\n*This project is currently in **alpha**. The API should be considered unstable and likely to change*\n\n**protoc-gen-defaults** is a protoc plugin generating the implementation of a `Defaulter` \ninterface on messages:\n```go\ntype Defaulter interface {\n\tDefault()\n}\n```\n\n## Installation\n\n```bash\ngo get github.com/onexstack/protoc-gen-defaults\n```\n\n## Usage\n\n### Overview\n\n**protoc-gen-defaults** make use of **Protobuf** options to define defaults field value.\n\n### Generation\n\n**protoc-gen-defaults** works the same way does the **protoc** plugins\n\nExample:\n```bash\nprotoc -I. -I defaults --go_out=paths=source_relative:. --defaults_out=paths=source_relative:. types.proto\n```\n\n### Disable generation or implementation\n\nImplementation generation can be ignored with the `(defaults.ignored) = true` message option.\n\n```proto\nmessage NoDefaulterImplementation {\n\toption (defaults.ignored) = true;\n\tstring string_field = 1 [(defaults.value).string = \"string_field\"];\n}\n```\n\u003e It may be useful if you intend to write your own `Defaulter` implementation.\n\n\nAn empty implementation can be generated with the `(defaults.disabled) = true` message option\n\n```proto\nmessage EmptyDefaulterImplementation {\n    option (defaults.disabled) = true;\n    string string_field = 1 [(defaults.value).string = \"string_field\"];\n}\n```\n\n### Scalar and Well-Known Value\n\nEach scalar or Well-Known type has its corresponding `(defaults.value).[scalar] = [value]` option, \nthe `[value]` will be set if the scalar field has the **zero value**, e.g `0` for numbers, \n`\"\"` for strings, `false` for bools\n\n- **float / google.protobuf.FloatValue**:\n    ```proto\n    float float = 1 [(defaults.value).float = 0.42];\n    ```\n- **double / google.protobuf.DoubleValue**: \n    ```proto \n    double double = 2 [(defaults.value).double = 0.42];\n    ****google.protobuf.DoubleValue double_value = 20 [(defaults.value).double = 0.42];\n    ```\n- **int32 / google.protobuf.Int32Value**: \n    ```proto  \n    int32 int32 = 3 [(defaults.value).int32 = 42];\n    google.protobuf.Int32Value int32_value = 24 [(defaults.value).int32 = 42];\n    ```\n- **int64 / google.protobuf.Int64Value**: \n    ```proto  \n    int64 int64 = 4 [(defaults.value).int64 = 42];\n    google.protobuf.Int64Value int64_value = 22 [(defaults.value).int64 = 42];\n    ```\n- **uint32 / google.protobuf.UInt32Value**: \n    ```proto  \n    uint32 uint32 = 5 [(defaults.value).uint32 = 42];\n    google.protobuf.UInt32Value uint32_value = 25 [(defaults.value).uint32 = 42];\n    ```\n- **uint64 / google.protobuf.UInt64Value**: \n    ```proto  \n    uint64 uint64 = 6 [(defaults.value).uint64 = 42];\n    google.protobuf.UInt64Value uint64_value = 23 [(defaults.value).uint64 = 42];\n    ```\n- **sint32**: \n    ```proto  \n    sint32 sint32 = 7 [(defaults.value).sint32 = 42];\n    ```\n- **sint64**: \n    ```proto  \n    sint64 sint64 = 8 [(defaults.value).sint64 = 42];\n    ```\n- **fixed32**: \n    ```proto  \n    fixed32 fixed32 = 9 [(defaults.value).fixed32 = 42];\n    ```\n- **fixed64**: \n    ```proto  \n    fixed64 fixed64 = 10 [(defaults.value).fixed64 = 42];\n    ```\n- **sfixed32**: \n    ```proto  \n    sfixed32 sfixed32 = 11 [(defaults.value).sfixed32 = 42];\n    ```\n- **sfixed64**: \n    ```proto  \n    sfixed64 sfixed64 = 12 [(defaults.value).sfixed64 = 42];\n    ```\n- **bool / google.protobuf.BoolValue**: \n    ```proto  \n    bool bool = 13 [(defaults.value).bool = true];\n    google.protobuf.BoolValue bool_value = 26 [(defaults.value).bool = false];\n    ```\n- **string / google.protobuf.StringValue**: \n    ```proto  \n    string string = 14 [(defaults.value).string = \"42\"];\n    google.protobuf.StringValue string_value = 27 [(defaults.value).string = \"42\"];\n    ```\n- **bytes / google.protobuf.BytesValue**: \n    ```proto  \n    bytes bytes = 15 [(defaults.value).bytes = \"42\"];\n    google.protobuf.BytesValue bytes_value = 28 [(defaults.value).bytes = \"42\"];\n    ```\n    \n### Messages \n\nMessage default behaviour is defined with the `(defaults.value).message = {initialize: bool, defaults: bool}` field option.\n\n- `initialize`: if set to `true` the field will be initialized with an empty `stuct reference` from the appropriate type\n- `defaults`: tells that the `Default` method should be called if the type implements the `Defaulter` interface\n\n```proto\nMessage message = 17 [(defaults.value).message = {initialize: true, defaults: true}];\n```\n\n### Well-Known Messages\n\n**google.protobuf.Duration** \n  \nThe default value is parsed according to the [Prometheus time durations format](https://prometheus.io/docs/prometheus/latest/querying/basics/#time-durations)\n```proto\ngoogle.protobuf.Duration duration = 18 [(defaults.value).duration = \"2d\"];\n```\n\n**google.protobuf.Timestamp**\n\nThe default value is parsed using the following RFCs as defined in the `time` package:\n  - time.RFC822\n  - time.RFC822Z\n  - time.RFC850\n  - time.RFC1123\n  - time.RFC1123Z\n  - time.RFC3339\n\nTimestamp also support a convenient value `now` which will set the value from `time.Now()` at `Default()` method call time.\n\n```proto\ngoogle.protobuf.Timestamp timestamp = 19 [(defaults.value).timestamp = \"now\"];\ngoogle.protobuf.Timestamp time_value_field_with_default = 18 [(defaults.value).timestamp = \"1952-03-11T00:00:00Z\"];\n```\n\n### Enums\n\nThe enum index value if currently to zero.\n\n```proto\nenum Enum {\n    NONE = 0;\n    ONE = 1;\n    TWO = 2;\n}\nEnum enum = 16 [(defaults.value).enum = 1];\n```\n\n### oneof\n\nIf the `defaults.oneof` option is set, the `oneof` will be initialized with a struct of the *oneof type wrapper*, \nthe regular field type `default` option will be applied.\n\n```proto\noneof one_of {\n    option (defaults.oneof) = \"two\";\n    OneOfOne one = 29 [(defaults.value).message = {defaults: true, initialize: true}];\n    OneOfTwo two = 30 [(defaults.value).message = {defaults: true, initialize: true}];\n    OneOfThree three = 31 [(defaults.value).message = {defaults: true, initialize: true}];\n    Enum four = 32 [(defaults.value).enum = 1];\n}\n```\n\n\n### Repeated and Maps\n\n`repeated` and `maps` are not supported.\n\n\n## All types example\n```proto\nsyntax = \"proto3\";\n\npackage tests;\n\noption go_package = \"github.com/onexstack/protoc-gen-defaults/tests/pb;pb\";\n\nimport \"defaults/defaults.proto\";\n\nimport \"google/protobuf/wrappers.proto\";\nimport \"google/protobuf/timestamp.proto\";\nimport \"google/protobuf/duration.proto\";\n\nmessage Types {\n    // Scalar Field Types\n    float float = 1 [(defaults.value).float = 0.42];\n    double double = 2 [(defaults.value).double = 0.42];\n    int32 int32 = 3 [(defaults.value).int32 = 42];\n    int64 int64 = 4 [(defaults.value).int64 = 42];\n    uint32 uint32 = 5 [(defaults.value).uint32 = 42];\n    uint64 uint64 = 6 [(defaults.value).uint64 = 42];\n    sint32 sint32 = 7 [(defaults.value).sint32 = 42];\n    sint64 sint64 = 8 [(defaults.value).sint64 = 42];\n    fixed32 fixed32 = 9 [(defaults.value).fixed32 = 42];\n    fixed64 fixed64 = 10 [(defaults.value).fixed64 = 42];\n    sfixed32 sfixed32 = 11 [(defaults.value).sfixed32 = 42];\n    sfixed64 sfixed64 = 12 [(defaults.value).sfixed64 = 42];\n    bool bool = 13 [(defaults.value).bool = true];\n    string string = 14 [(defaults.value).string = \"42\"];\n    bytes bytes = 15 [(defaults.value).bytes = \"42\"];\n\n    // Complex Field Types\n    enum Enum {\n        NONE = 0;\n        ONE = 1;\n        TWO = 2;\n    }\n    Enum enum = 16 [(defaults.value).enum = 1];\n    Message message = 17 [(defaults.value).message = {initialize: true, defaults: false}];\n    oneof one_of {\n        option (defaults.oneof) = \"two\";\n        OneOfOne one = 29 [(defaults.value).message = {defaults: true, initialize: true}];\n        OneOfTwo two = 30 [(defaults.value).message = {defaults: true, initialize: true}];\n        OneOfThree three = 31 [(defaults.value).message = {defaults: true, initialize: true}];\n        Enum four = 32 [(defaults.value).enum = 1];\n    }\n    \n    // WellKnow types\n    google.protobuf.Duration duration = 18 [(defaults.value).duration = \"2d\"];\n    google.protobuf.Timestamp timestamp = 19 [(defaults.value).timestamp = \"now\"];\n    google.protobuf.DoubleValue double_value = 20 [(defaults.value).double = 0.42];\n    google.protobuf.FloatValue float_value = 21 [(defaults.value).float = 0.42];\n    google.protobuf.Int64Value int64_value = 22 [(defaults.value).int64 = 42];\n    google.protobuf.UInt64Value uint64_value = 23 [(defaults.value).uint64 = 42];\n    google.protobuf.Int32Value int32_value = 24 [(defaults.value).int32 = 42];\n    google.protobuf.UInt32Value uint32_value = 25 [(defaults.value).uint32 = 42];\n    google.protobuf.BoolValue bool_value = 26 [(defaults.value).bool = false];\n    google.protobuf.StringValue string_value = 27 [(defaults.value).string = \"42\"];\n    google.protobuf.BytesValue bytes_value = 28 [(defaults.value).bytes = \"42\"];\n}\n\nmessage Message {\n    string field = 1 [(defaults.value).string = \"lonely field\"];\n}\n\nmessage OneOfOne {\n    option (defaults.ignored) = true;\n    string string_field = 1 [(defaults.value).string = \"string_field\"];\n}\n\nmessage OneOfTwo {\n    string string_field = 1 [(defaults.value).string = \"string_field\"];\n}\n\nmessage OneOfThree {\n    option (defaults.disabled) = true;\n    string string_field = 1 [(defaults.value).string = \"string_field\"];\n}\n\n```\n\n### Using reflection / without code generation\n\nSetting protobuf message defaults is also supported using reflection:\n\n```go\npackage main\n\nimport (\n\tpb \"...\"\n\t\"github.com/onexstack/protoc-gen-defaults/defaults\"\n)\n\nfunc main() {\n\tvar msg pb.MyMessage\n\tdefaults.Apply(\u0026msg)\n}\n\n```\n\n## TODO\n- [x] docs\n- [x] oneof support\n- [x] set default values by using [Protobuf reflection](https://pkg.go.dev/google.golang.org/protobuf@v1.27.1/reflect/protoreflect)\n- [ ] add more generic methods to use as default value, e.g. *uuid*, *bsonid*... ?\n- [ ] repeated support ?\n- [ ] maps support ?\n- [x] bytes support\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonexstack%2Fprotoc-gen-defaults","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonexstack%2Fprotoc-gen-defaults","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonexstack%2Fprotoc-gen-defaults/lists"}