{"id":26508629,"url":"https://github.com/rodaine/protogofakeit","last_synced_at":"2025-03-21T00:37:11.623Z","repository":{"id":217795169,"uuid":"744829052","full_name":"rodaine/protogofakeit","owner":"rodaine","description":"Produce fake data for protocol buffer messages","archived":false,"fork":false,"pushed_at":"2025-02-10T16:01:03.000Z","size":69,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T17:21:38.297Z","etag":null,"topics":["golang","proto","protobuf","protocol-buffers","testing"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/rodaine/protogofakeit","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/rodaine.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":"2024-01-18T04:53:00.000Z","updated_at":"2025-02-10T16:01:06.000Z","dependencies_parsed_at":"2024-01-22T22:10:47.496Z","dependency_job_id":"d9ff209f-379e-4571-9275-320686a81d40","html_url":"https://github.com/rodaine/protogofakeit","commit_stats":null,"previous_names":["rodaine/protogofakeit"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodaine%2Fprotogofakeit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodaine%2Fprotogofakeit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodaine%2Fprotogofakeit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rodaine%2Fprotogofakeit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rodaine","download_url":"https://codeload.github.com/rodaine/protogofakeit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244717385,"owners_count":20498284,"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":["golang","proto","protobuf","protocol-buffers","testing"],"created_at":"2025-03-21T00:37:11.236Z","updated_at":"2025-03-21T00:37:11.615Z","avatar_url":"https://github.com/rodaine.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# protogofakeit [![Go Reference](https://pkg.go.dev/badge/github.com/rodaine/protogofakeit.svg)](https://pkg.go.dev/github.com/rodaine/protogofakeit) [![CI](https://github.com/rodaine/protogofakeit/actions/workflows/ci.yaml/badge.svg)](https://github.com/rodaine/protogofakeit/actions/workflows/ci.yaml)\n\n`protogofakeit` provides a utility for producing fake data in Protocol Buffer \nmessages via [`gofakeit`][gofakeit].\n\n## Example\n\n`protogofakeit` works by annotating fields of protobuf messages with the \n`(gofakeit.generate)` custom options. Below, an example `User` and related \nmessages have been annotated:\n\n```protobuf\nsyntax=\"proto3\";\nimport \"gofakeit/gofakeit.proto\";\n\nmessage User {\n  fixed64 user_id = 1;\n  string first_name = 2 [(gofakeit.generate).tag = \"{firstname}\"];\n  string last_name = 3 [(gofakeit.generate).tag = \"{lastname}\"];\n  string email = 4 [(gofakeit.generate).tag = \"{email}\"];\n  Location location = 5;\n  repeated string hobbies = 6 [\n    (gofakeit.generate).repeated.range = { min: 1, max: 3},\n    (gofakeit.generate).repeated.element.tag = \"{hobby}\"\n  ];\n  map\u003cstring, PetType\u003e pets = 7 [\n    (gofakeit.generate).map.range = { min: 1, max: 3},\n    (gofakeit.generate).map.key.tag = \"{petname}\"\n  ];\n}\n\nenum PetType {\n  PET_TYPE_UNSPECIFIED = 0;\n  PET_TYPE_DOG = 1;\n  PET_TYPE_CAT = 2;\n}\n\nmessage Location {\n  double latitude = 1 [(gofakeit.generate).tag = \"{latitude}\"];\n  double longitude = 2 [(gofakeit.generate).tag = \"{longitude}\"];\n}\n```\n\n`protogofakeit` does **NOT** require any code generation (beyond `protoc-gen-go`) to populate a message:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/brianvoe/gofakeit/v6\"\n\t\"github.com/rodaine/protogofakeit\"\n\t\"github.com/rodaine/protogofakeit/example/gen\"\n\t\"google.golang.org/protobuf/encoding/prototext\"\n)\n\nfunc main() {\n\tfaker := gofakeit.New(3)\n\tprotoFaker := protogofakeit.New(faker)\n\tuser := \u0026gen.User{}\n\tprotoFaker.FakeProto(user)\n\tfmt.Println(prototext.Format(user))\n}\n```\n\nOutputs:\n\n```text\nuser_id: 15864040051628833669\nfirst_name: \"Philip\"\nlast_name: \"Casper\"\nemail: \"tyreekstroman@mitchell.biz\"\nlocation: {\n  latitude: 1.383491\n  longitude: -62.968324\n}\nhobbies: [\n  \"Curling\"\n  \"Slot car\"\n  \"Fishkeeping\"\n]\npets: {\n  \"Archie\": PET_TYPE_DOG\n}\n```\n\n## Annotating Messages\n\nThe `protogofakeit` proto files must be imported into your proto files to set \nthe custom options on fields.\n\n### protoc CLI\n\nIf you are using [`protoc`][protoc], the `proto` directory must be in the import path to compile:\n\n```shell\nprotoc \\\n  -I \"$PROTOGOFAKEIT_PATH/proto\" \\\n  --go_out=\"$OUTPUT_DIR\" \\\n  $PROTOS\n```\n\n### buf CLI\n\nIf you are using [`buf`][buf], add a dependency on `buf.build/rodaine/protogofakeit` \nto your module's [`buf.yaml`][buf.yaml].\n\n```yaml\n# buf.yaml\nversion: v1\ndeps:\n  - buf.build/rodaine/protogofakeit\n# ...\n```\n\nRemember to run `buf mod update` to update the lockfile. If you are using buf \n[managed mode][managed] for code generation, ensure the correct import path is used for\n`protogofakeit`:\n\n```yaml\n# buf.gen.yaml\nversion: v1\nmanaged:\n  enabled: true\n  go_package_prefix:\n    override:\n      buf.build/rodaine/protogofakeit: github.com/rodaine/protogofakeit/gen\n# ...\n```\n\n### Default Generation\n\nBy default, `protogofakeit` populates all fields of a message with random values:\n\n- **numbers**: a uniform, random value in the full range of the underlying type.\n- **bool**: a random `true` or `false`\n- **string**: a random ASCII string between 4–10 characters, inclusive.\n- **bytes**: random slice of 4–10 bytes (inclusive) in the ASCII range.\n- **enums**: a random defined value of the enum (including any value assigned to 0).\n- **messages**: the field is set and its fields are populated with random data, \n  to the max recursion depth (default of 5).\n- **repeated**: a slice of 4–10 (inclusive) elements populated with random values.\n- **map**: a map of 4–10 (inclusive) key-value pairs populated with random keys \n  and values. Note that the total pairs may be less if there is a key collision \n  during generation.\n- **oneof**: a random field (or no field) contained in the oneof will be set to \n  its random value.\n- **optional**: optional fields are always set.\n- **google.protobuf.Timestamp**: a random valid timestamp value\n- **google.protobuf.Duration**: a random valid duration value\n\nDefault sizes of string, bytes, repeated, and map fields as well as the maximum \nrecursion depth can be customized when initializing the `ProtoFaker` instance \nvia `Option` values. See the documentation for more details. \n\n### Skip\n\nFake generation can be skipped on any field by annotating it with `skip`:\n\n```protobuf\nmessage Skipped {\n  string value = 1 [(gofakeit.generate).skip = true];\n}\n```\n\nScalar values will default to their zero value, while message, repeated, and map\nfields will be empty/unset.\n\n### Tags\n\nThe primary way of customizing field generation is via tags, which are identical\nto the [struct tags][struct] used to customize plain-old Go structs for `gofakeit`.\nTags act as simplified templates using curly braces (`{...}`) to specify \"function\" \ncalls to the faker.\n\nAfter resolving any functions, the `tag` value is then parsed to match the type of \nthe associated field. Any parse errors result in fake generation to fail:\n\n- **integers**: either `strconv.ParseInt(tag, 0, n)` or `strconv.ParseUint(tag, 0, n)` \n  if unsigned, where `n` is the bitsize of the field.\n- **floats**: `strconv.ParseFloat(tag, n)`, where `n` is the bitsize of the field.\n- **bool**: `strconv.ParseBool(tag)`\n- **string**: as-is\n- **bytes**: `[]byte(tag)`\n- **enums**: `strconv.ParseInt(tag, 0, 32)`. Undefined enum values are supported/possible.\n- **google.protobuf.Timestamp**: `time.Parse(format, tag)`, where `format` is \n  a `time.Layout` based format (default of `time.RFC3339Nano`).\n- **google.protobuf.Duration**: `time.ParseDuration(tag)`\n\nTags are ignored on message, repeated, and map fields. The timestamp parse format\ncan be customized when initializing the `ProtoFaker` instance.\n\n```protobuf\nmessage Tags {\n  string name = 1 [(gofakeit.generate).tag = \"{firstname}\"]; // random\n  string foo = 2 [(gofakeit.generate).tag = \"bar\"]; // constant \"bar\"\n  int32 age = 3 [(gofakeit.generate).tag = \"{intrange:13,99}\"]; // function parameters\n  string pet = 4[(gofakeit.generate).tag = \"{petname} ({animal})\"]; // mixed\n}\n```\n\n[Custom tag functions][custom] can be registered on the underlying faker, though \nthis is strongly discouraged as it makes the tags less portable.\n\n### Templates\n\nFor more complex fake generation, Go [templates] are supported, enabling loops, \nconditionals, and other logic alongside the functions available from the \nunderlying faker. The resulting value is parsed the same as tags above. For long \ntemplates, a series of strings can be used which are automatically concatenated \nby the protobuf compiler. \n\n```protobuf\nmessage Templates {\n  string cmd = 1 [\n    (gofakeit.generate).template = \n      '{{ $lang := RandomString (SliceString \"go\" \"python\" \"javascript\" }}'\n      '{{ if eq $lang \"go\" }}go get'\n      '{{ else if eq $lang \"python\" }}pip install'\n      '{{ else }}npm install'\n      '{{ end }} ({{ $lang }})'\n  ];\n}\n```\n\nTemplates are orders of magnitude slower than tags, so only reach for them if \nyou need something more powerful. Custom template functions can be registered \nwhen configuring the `ProtoFaker` instance, though this is strongly discouraged \nas it makes the templates less portable.\n\n### Repeated / Map Fields\n\nRepeated (list) and map fields can be customized beyond the defaults, including \ntheir size and items.\n\n#### Size\n\nA repeated or map field's size can be specified via either a constant `len` or a\nrandom `range`.\n\n```protobuf\nmessage Sizes {\n  repeated string a = 1 [\n    (gofakeit.generate).repeated.len = 3 // exactly 3 elements\n  ]; \n  map\u003cstring, int32\u003e b = 2 [\n    (gofakeit.generate).map.range = { min: 0, max: 2 } // 0-2 key-value pairs, inclusive.\n  ];  \n}\n```\n\n#### Items\n\nA repeated field's elements or a map field's keys and values can have options \nassociated with them just like singular fields. Note that message items still \nignore any `tag` or `template` options. \n\n```protobuf\nmessage Elements {\n  repeated string a = 1 [\n    (gofakeit.generate).repeated.element.tag = \"{animal}\"\n  ];\n  map\u003cstring, int32\u003e b = 2 [\n    (gofakeit.generate).map.key.tag = \"{petname}\",\n    (gofakeit.generate).map.value.tag = \"{intrange:0,100}\"\n  ];\n}\n```\n\n[gofakeit]: https://github.com/brianvoe/gofakeit\n[protoc]: https://protobuf.dev/programming-guides/proto3/#generating\n[buf]: https://buf.build/docs/ecosystem/cli-overview\n[buf.yaml]: https://buf.build/docs/configuration/v1/buf-yaml\n[managed]: https://buf.build/docs/generate/managed-mode\n[struct]: https://github.com/brianvoe/gofakeit/tree/master#struct\n[custom]: https://github.com/brianvoe/gofakeit/tree/master#custom-functions\n[templates]: https://github.com/brianvoe/gofakeit/tree/master#templates","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodaine%2Fprotogofakeit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frodaine%2Fprotogofakeit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frodaine%2Fprotogofakeit/lists"}