-
Notifications
You must be signed in to change notification settings - Fork 907
TypeSpec Go QuickStart
Go Azure SDK Design Guidelines is the overall design guideline of sdk. Make sure you are familiar with concepts such as "ServiceClient" and "Packages".
Make sure you are familiar with Git and Go Packages.
Namespace is decided by SDK arch board.
For service
, it is usually your service name in REST API specifications. For instance, if the TypeSpec is under specification/storage
, the service would normally be storage
.
For package
, please refer to Azure SDK Module Design in Go Guideline. For example, if the service name is storage
, the package should be armstorage
or azstorage
.(Use arm for management-plane packages, and az for all other packages.)
After service
and module
, the output-dir
would be <sdk-repository-dir>/sdk/<service>/<module>
. This is where the SDK code should be generated when you call npx tsp compile
.
☑️ It is recommended to configure TypeSpec package on REST API specifications. Please refer to these guidelines.
💡Particularly, here are templates for management plane and data plane service, you can use these templates:
☑️ Please make sure service-dir
, package-dir
, module
(for typespec-go) is correctly configured.
-
service-dir
: Default assdk/<service>
. If your service is a management-plane service, likespecification/azurefleet/AzureFleet.Management
, the service dir needs to besdk/resourcemanager/computefleet
. -
package-dir
: Default as<package>
. -
module
: Default as "github.com/Azure/azure-sdk-for-go/{service-dir}/{package-dir}". -
module-version
: Semantic version of the Go module(e.g. 1.2.3). -
fix-const-stuttering
: Default astrue
. Fix stuttering forconst
types and values. -
flavor
: Default asazure
for azure service. -
generate-samples
: Default astrue
. Generates sample codes with the SDK.. -
generate-fakes
: Default astrue
. Generates test codes with the SDK.. -
head-as-boolean
: Default astrue
. HEAD requests will return a boolean value based on the HTTP status code. -
inject-spans
: Default astrue
. Enables generation of spans for distributed tracing. -
factory-gather-all-params
: Default asfalse
. TheNewClientFactory
constructor gathers all parameters or only common parameters of clients. -
azcore-version
: Semantic version of azcore without the leading 'v' to use if different from the default version (e.g. 1.2.3). -
disallow-unknown-fields
: Default asfalse
. When true, unmarshalers will return an error when an unknown field is encountered in the payload. -
file-prefix
: Optional prefix to file names. For example, if you set your file prefix to "zzz_", all generated code files will begin with "zzz_". -
rawjson-as-bytes
: Default asfalse
. When true, properties that are untyped (i.e. raw JSON) are exposed as []byte instead of any or map[string]any. The default is false. -
slice-elements-byval
: Default asfalse
. When true, slice elements will not be pointer-to-type. -
single-client
: Indicates package has a single client. This will omit the Client prefix from options and response types. If multiple clients are detected, an error is returned. The default is false. -
stutter
: Uses the specified value to remove stuttering from types and funcs instead of the built-in algorithm. -
fix-const-stuttering
: Default asfalse
. When true, fix stuttering forconst
types and values.
After configuration is completed, making a draft pull request on azure-rest-api-specs repository would automatically trigger SDK generation for all configured SDKs. The automation will create new pull request on azure-sdk-for-go repository. One can fork it for further development.
You can refer to the tsp-location.yaml which describes the supported properties in the file. You can update tsp-location.yaml
under SDK project folder to track the TypeSpec project.
SDK will be generated under the SDK project folder of azure-sdk-for-go
.
With tsp-location.yaml
ready, run the following tsp-client
command from project directory (i.e. your current directory is <sdk-repository-dir>/sdk/<service>/<module>
) to generate the code:
:ballot_box_with_check: tsp-client
can be installed globally via npm install -g @azure-tools/typespec-client-generator-cli
.
tsp-client update
The version of TypeSpec-Go is configured in emitter-package.json. Change it in local, if you would like to use a different version of typespec-go
.
If you would like to test with locally modified TypeSpec, invoke tsp-client update
with --save-inputs
option. This tells the command not to delete the "TempTypeSpecFiles" folder, therefore you can modify the TypeSpec and then run tsp-client generate --save-inputs
. Remember that all changes in "TempTypeSpecFiles" folder would be temporary.