Best Practices
Follow these best practices when using the Atlas Go SDK.
Using Getters Instead of Direct Field Access
When accessing responses, use the getter function instead of direct field access.
For example, use response.GetField()
instead of response.Field
.
Using getter functions allows seamless pointer handling and can help prevent panic errors.
Additionally, the Atlas Go SDK provides Set
, IsSet
and Unset
methods for safe field modifications.
Check for Empty Strings for String Pointers
When a model contains a pointer to a string
, the Atlas Go SDK sends that value to the server,
even if it is set to an empty string ( ""
).
Instead of direct assignment:
// Surrounding code omitted for brevity test := "" requestBody.StringPointerValue = test
Users should always check for empty strings before assigning them:
// Surrounding code omitted for brevity if test != "" requestBody.StringPointerValue = test
Working with Date Fields
In the Atlas Go SDK, the *time.Time
type represents date fields for handling time-related data.
When you compare values based on time.Time
, either never compare pointers directly or do the following:
Avoid using direct comparison operators (e.g.,
myStruct.MyDateField == ""
) to check for equality. Comparing pointers directly will check if they refer to the same memory address rather than comparing the actual date values. Since eachtime.Time
instance is allocated in different memory locations, direct comparisons might yield unexpected results.Use the
Has
function to check non-nil pointers: The SDK provides a dedicatedHasFieldName
orGetFieldName
function for each model to check if atime.Time
pointer is non-nil before accessing its value. Always use this function to ensure that the pointer is valid before you perform any operations.Use
time.Time
methods to compare date values: When you have confirmed that thetime.Time pointer
is non-nil, you can safely usetime.Time
methods to compare the actual date values. Commonly used methods for comparison includeBefore
,After
, andEqual
:
// Surrounding code omitted for brevity if !sdkModel.HasSomeDateField() { return; } datePtr1 := sdkModel.SomeDateField; if datePtr1.Before(*datePtr2) { // datePtr1 is before datePtr2. } else if datePtr1.After(*datePtr2) { // datePtr1 is after datePtr2. } else { // datePtr1 and datePtr2 are equal. }
Working with Objects
The Atlas Go SDK represents free-form objects with the golang ìnterface{}
type in models and parameters. Free-form objects include objects of any shape.
The golang ìnterface{}
type allows you to use the following types as input arguments:
Basic types (integer, boolean)
Your own structures
map[string]interface{} for free-form objects
NOTE: The Atlas Go SDK uses free-form objects only where models need to handle multiple conflicting values. For example, some of the existing Atlas Search APIs allow you to supply booleans or objects.
Working with Pointers
The Atlas Go SDK utilizes SDK pointers to denote optional values in the Go programming language:
// Surrounding code omitted for brevity type Data struct { // Represents an optional name Name *string `json:"results,omitempty"` }
In the example above, the string value is optional, and it won't be sent to the server if you don't explicitly set it.
Working with Arrays
The Atlas Go SDK represents all arrays as pointers:
// Surrounding code omitted for brevity type Data struct { Results *[]DataRole `json:"results,omitempty"` }
The following scenarios use pointers with arrays:
Update a request containing an empty array (resetting the field values):
If you explicitly set a struct property to an empty array, the SDK will send an empty array request to the Atlas API.
// Surrounding code omitted for brevity data := Data{ // Sending an empty array Results: &[]DataRole{} }
Update a request without updating the array field:
When performing an update operation, we recommend that you don't set the struct property.
// Surrounding code omitted for brevity data := Data{ // Sending an empty array by not setting field values (value is nil) // Results: &[]DataRole{} }
These practices ensure accurate handling of optional values and array updates in the SDK when you work with pointers in Golang.
Working with Binary Responses
In the Atlas Go SDK, the io.ReadCloser
type is used to return binary data using the APIs.
Use
io.Copy
to store on a file or pass through another steam.Use
io.ReadAll
to read all bytes in memory.Call the
.Close()
function after reading the data
Note: see example in examples/download/downloadLogs.go
Use Method for Creating Models
Use dedicated methods for creating new models.
For example, instead of using the following:
// Surrounding code omitted for brevity GroupInvitationUpdateRequest{ ... }
Use the following dedicated method:
// Surrounding code omitted for brevity admin.NewGroupInvitationUpdateRequest(...)
Use golangci-lint Validators
Use golangci-lint to detect common errors when using the Atlas Go SDK. The Atlas Go SDK doesn't provide its own linter.
Linting Issues
We don't recommend using the bodyclose
rule as it reports an many false positives with Atlas GO SDK and other libraries.
To learn more, see bodyclose.