{"id":13629329,"url":"https://github.com/ladeak/ProtobufSourceGenerator","last_synced_at":"2025-04-17T08:34:39.755Z","repository":{"id":65548594,"uuid":"587048578","full_name":"ladeak/ProtobufSourceGenerator","owner":"ladeak","description":"A source generator that generates partial helper classes where member properties are attributed with ProtoMember attribute.","archived":false,"fork":false,"pushed_at":"2025-04-15T05:47:08.000Z","size":137,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-15T06:32:32.069Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","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/ladeak.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,"zenodo":null}},"created_at":"2023-01-09T20:54:41.000Z","updated_at":"2025-04-15T05:47:06.000Z","dependencies_parsed_at":"2023-11-07T07:27:01.762Z","dependency_job_id":"570671ce-1e8c-4f03-80f0-041b01925743","html_url":"https://github.com/ladeak/ProtobufSourceGenerator","commit_stats":{"total_commits":44,"total_committers":2,"mean_commits":22.0,"dds":"0.15909090909090906","last_synced_commit":"749d7c27dfdf2b351ccbf31a534c566b54e16c38"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladeak%2FProtobufSourceGenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladeak%2FProtobufSourceGenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladeak%2FProtobufSourceGenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladeak%2FProtobufSourceGenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ladeak","download_url":"https://codeload.github.com/ladeak/ProtobufSourceGenerator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249023183,"owners_count":21199953,"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-08-01T22:01:07.604Z","updated_at":"2025-04-17T08:34:39.473Z","avatar_url":"https://github.com/ladeak.png","language":"C#","readme":"# Protobuf Source Generator\n\nA source generator that generates partial helper classes where member properties are attributed with ProtoMember attribute for serialization with [protobuf-net](https://github.com/protobuf-net/protobuf-net).\n\n## Getting Started\n\nInstall nuget package:\n\n```\ndotnet add package LaDeak.ProtobufSourceGenerator\n```\n\nThe source generator creates partial classes with *private* properties that are attributed with `[ProtoMember]` attributes. The properties *get* and *set* a corresponding property from the source type. This way developers are free to add and remove properties without explicitly attributing them.\n\n\u003e Note that adding, removing or reordering properties might cause breaking changes for the serialized data, as the tags assigned with `[ProtoMember]` attribute are given based on the source type's definition.\n\nTo generate a partial type for a custom type, mark the type with `[ProtoContract]` attribute and with `partial` modifier. For example, the following entity type can be made source generating:\n\n```csharp\npublic class Entity\n{\n    public int Id { get; set; }\n}\n```\n\nAdd `[ProtoContract]` attribute and `partial` modifier on the type definition:\n\n\n```csharp\n[ProtoContract]\npublic partial class Entity\n{\n    public int Id { get; set; }\n}\n```\n\nWith this change a corresponding partial type is generated, that can be used for serialization with [protobuf-net](https://github.com/protobuf-net/protobuf-net):\n\n```csharp\n#nullable enable\nnamespace SampleApp;\npublic partial class Entity\n{\n    [ProtoBuf.ProtoMember(1)]\n    private int ProtoId { get =\u003e Id; set =\u003e Id = value; }\n}\n```\n\nThe source generator generates serializable properties that are auto properties with getter and setters. \n\n## Non-Generating Properties\n\n- `init` properties are excluded from source generation\n- Non-auto properties are not generated\n- Properties marked with `[ProtoIgnore]` and `[ProtoMemeber(x)]` attributes are not generated.\n- Positional Records (not supported by protobuf-net)\n\nIn case a property needs to be serialized, but it has no corresponding generated property, it may be attributed with `[ProtoMemeber(x)]` on the original type. The source generator makes sure that tag *x* is not used on the generated partial type.\n\n## Nested Types\n\nGenerating partial types for nested types is supported, however parent types must be marked with *partial* modifier.\n\n## Analyzer\n\nThe source generator also comes with an analyzer helping source generation:\n\n- issues an error if a custom type of a property is not participating in source generation\n- issues an error if a source generated nested type's parent is not partial\n- issues an info if a property is not part of source generation\n- issues a warning when the base type is not attributed for serialization\n\n## Advanced Usage\n\n### Null Items in Collections\n\nSerializing null values by protobuf-net is not allowed:\n\n```csharp\nnew List\u003cstring\u003e() { \"one\", null, \"three\" };\n```\n\nThe generator will not remove null items, so in this case an exception shall be handled by the user code.\n\n### Empty Collections\n\n```csharp\npublic List\u003cstring\u003e Value { get; set; }\n```\n\nEmpty lists are not distinguished by the proto contract from a null lists. protobuf-net suggests to have an additional `bool` property indicating if the list was empty at serialization or not, and based on the value instantiate a collection at deserialization or not.\n\nProtoBufGenearator generates the helper property such as:\n\n```csharp\n[global::ProtoBuf.ProtoMember(1)]\nprivate System.Collections.Generic.List\u003cstring\u003e ProtoValue { get =\u003e Value; set =\u003e Value = value; }\n\n[global::ProtoBuf.ProtoMember(2)]\n[global::System.ComponentModel.DataAnnotations.Schema.NotMappedAttribute]\nprivate bool ProtoIsEmptyValue\n{\n    get =\u003e ProtoValue?.Count == 0;\n    set\n    {\n        if (value)\n            ProtoValue = new();\n    }\n}\n```\n\n### Initialized to Enumerable Empty\n\n```csharp\npublic IEnumerable\u003cstring\u003e Values { get; set; } = Enumerable.Empty\u003cstring\u003e();\n```\n\nCollections when initialized with enumerable empty cause an issue with deserialization as protobuf-net will attempt to add an item to the existing collection, which is not possible in the above case.\n\nIn this case the user may initialze with an empty list, or manually decorate the property with `[ProtoMember()]` (or `[ProtoIgnore]`) attribute that will exclude it from protobuf source generation.\n\n### Custom Attributes on Properties\n\nIt is supported to decorate all properties in the generated partial class with custom attributes.\n\n```csharp\n[ProtoContract]\n[GeneratorOptions(PropertyAttributeType = typeof(NotMappedAttribute))]\npublic partial class CustomAttributedEntity\n{\n    // ...\n}\n```\n\nIn the above example, with the type parameter of `GeneratorOptions` the generator is instructed to apply `NotMappedAttribute` attribute on all generated properties on a class.\n\nFor a more fine grained approach consider excluding the given property from the source generation. For such cases use `[ProtoMember()]` or `[ProtoIgnore]` attributes.\n\n### Base Classes and Inheritance\n\nFollow the instructions of protobuf-net library. Apply `[ProtoInclude(...)]` attribute on the type definition.\n\n","funding_links":[],"categories":["Content","Source Generators"],"sub_categories":["61. [ProtobufSourceGenerator](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ProtobufSourceGenerator) , in the [Serializer](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#serializer) category","Serialization"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fladeak%2FProtobufSourceGenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fladeak%2FProtobufSourceGenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fladeak%2FProtobufSourceGenerator/lists"}