xref: /aosp_15_r20/external/bazelbuild-rules_go/docs/go/extras/extras.md (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2
3
4  [gazelle rule]: https://github.com/bazelbuild/bazel-gazelle#bazel-rule
5  [golang/mock]: https://github.com/golang/mock
6  [core go rules]: /docs/go/core/rules.md
7
8# Extra rules
9
10This is a collection of helper rules. These are not core to building a go binary, but are supplied
11to make life a little easier.
12
13## Contents
14- [gazelle](#gazelle)
15- [gomock](#gomock)
16- [go_embed_data](#go_embed_data)
17
18## Additional resources
19- [gazelle rule]
20- [golang/mock]
21- [core go rules]
22
23------------------------------------------------------------------------
24
25gazelle
26-------
27
28This rule has moved. See [gazelle rule] in the Gazelle repository.
29
30
31
32
33
34
35<a id="#go_embed_data"></a>
36
37## go_embed_data
38
39<pre>
40go_embed_data(<a href="#go_embed_data-name">name</a>, <a href="#go_embed_data-flatten">flatten</a>, <a href="#go_embed_data-package">package</a>, <a href="#go_embed_data-src">src</a>, <a href="#go_embed_data-srcs">srcs</a>, <a href="#go_embed_data-string">string</a>, <a href="#go_embed_data-unpack">unpack</a>, <a href="#go_embed_data-var">var</a>)
41</pre>
42
43**Deprecated**: Will be removed in rules_go 0.39.
44
45`go_embed_data` generates a .go file that contains data from a file or a
46list of files. It should be consumed in the srcs list of one of the
47[core go rules].
48
49Before using `go_embed_data`, you must add the following snippet to your
50WORKSPACE:
51
52``` bzl
53load("@io_bazel_rules_go//extras:embed_data_deps.bzl", "go_embed_data_dependencies")
54
55go_embed_data_dependencies()
56```
57
58`go_embed_data` accepts the attributes listed below.
59
60
61### **Attributes**
62
63
64| Name  | Description | Type | Mandatory | Default |
65| :------------- | :------------- | :------------- | :------------- | :------------- |
66| <a id="go_embed_data-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
67| <a id="go_embed_data-flatten"></a>flatten |  If <code>True</code> and <code>srcs</code> is used, map keys are file base names instead of relative paths.   | Boolean | optional | False |
68| <a id="go_embed_data-package"></a>package |  Go package name for the generated .go file.   | String | optional | "" |
69| <a id="go_embed_data-src"></a>src |  A single file to embed. This cannot be used at the same time as <code>srcs</code>.             The generated file will have a variable of type <code>[]byte</code> or <code>string</code> with the contents of this file.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional | None |
70| <a id="go_embed_data-srcs"></a>srcs |  A list of files to embed. This cannot be used at the same time as <code>src</code>.             The generated file will have a variable of type <code>map[string][]byte</code> or <code>map[string]string</code> with the contents             of each file. The map keys are relative paths of the files from the repository root. Keys for files in external             repositories will be prefixed with <code>"external/repo/"</code> where "repo" is the name of the external repository.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
71| <a id="go_embed_data-string"></a>string |  If <code>True</code>, the embedded data will be stored as <code>string</code> instead of <code>[]byte</code>.   | Boolean | optional | False |
72| <a id="go_embed_data-unpack"></a>unpack |  If <code>True</code>, sources are treated as archives and their contents will be stored. Supported formats are <code>.zip</code> and <code>.tar</code>   | Boolean | optional | False |
73| <a id="go_embed_data-var"></a>var |  Name of the variable that will contain the embedded data.   | String | optional | "Data" |
74
75
76<a id="gomock"></a>
77
78## gomock
79
80<pre>
81gomock(<a href="#gomock-name">name</a>, <a href="#gomock-library">library</a>, <a href="#gomock-out">out</a>, <a href="#gomock-source">source</a>, <a href="#gomock-interfaces">interfaces</a>, <a href="#gomock-package">package</a>, <a href="#gomock-self_package">self_package</a>, <a href="#gomock-aux_files">aux_files</a>, <a href="#gomock-mockgen_tool">mockgen_tool</a>,
82       <a href="#gomock-imports">imports</a>, <a href="#gomock-copyright_file">copyright_file</a>, <a href="#gomock-mock_names">mock_names</a>, <a href="#gomock-kwargs">kwargs</a>)
83</pre>
84
85Calls [mockgen](https://github.com/golang/mock) to generates a Go file containing mocks from the given library.
86
87If `source` is given, the mocks are generated in source mode; otherwise in reflective mode.
88
89
90**PARAMETERS**
91
92
93| Name  | Description | Default Value |
94| :------------- | :------------- | :------------- |
95| <a id="gomock-name"></a>name |  the target name.   |  none |
96| <a id="gomock-library"></a>library |  the Go library to took for the interfaces (reflecitve mode) or source (source mode).   |  none |
97| <a id="gomock-out"></a>out |  the output Go file name.   |  none |
98| <a id="gomock-source"></a>source |  a Go file in the given <code>library</code>. If this is given, <code>gomock</code> will call mockgen in source mode to mock all interfaces in the file.   |  <code>None</code> |
99| <a id="gomock-interfaces"></a>interfaces |  a list of interfaces in the given <code>library</code> to be mocked in reflective mode.   |  <code>[]</code> |
100| <a id="gomock-package"></a>package |  the name of the package the generated mocks should be in. If not specified, uses mockgen's default. See [mockgen's -package](https://github.com/golang/mock#flags) for more information.   |  <code>""</code> |
101| <a id="gomock-self_package"></a>self_package |  the full package import path for the generated code. The purpose of this flag is to prevent import cycles in the generated code by trying to include its own package. See [mockgen's -self_package](https://github.com/golang/mock#flags) for more information.   |  <code>""</code> |
102| <a id="gomock-aux_files"></a>aux_files |  a map from source files to their package path. This only needed when <code>source</code> is provided. See [mockgen's -aux_files](https://github.com/golang/mock#flags) for more information.   |  <code>{}</code> |
103| <a id="gomock-mockgen_tool"></a>mockgen_tool |  the mockgen tool to run.   |  <code>Label("//extras/gomock:mockgen")</code> |
104| <a id="gomock-imports"></a>imports |  dictionary of name-path pairs of explicit imports to use. See [mockgen's -imports](https://github.com/golang/mock#flags) for more information.   |  <code>{}</code> |
105| <a id="gomock-copyright_file"></a>copyright_file |  optional file containing copyright to prepend to the generated contents. See [mockgen's -copyright_file](https://github.com/golang/mock#flags) for more information.   |  <code>None</code> |
106| <a id="gomock-mock_names"></a>mock_names |  dictionary of interface name to mock name pairs to change the output names of the mock objects. Mock names default to 'Mock' prepended to the name of the interface. See [mockgen's -mock_names](https://github.com/golang/mock#flags) for more information.   |  <code>{}</code> |
107| <a id="gomock-kwargs"></a>kwargs |  <p align="center"> - </p>   |  none |
108
109
110