Follow these best practices when using the Atlas Go SDK.
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.
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:
test := ""
requestBody.StringPointerValue = test
Users should always check for empty strings before assigning them:
if test != ""
requestBody.StringPointerValue = test
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
:
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.
}
The Atlas Go SDK utilizes SDK pointers to denote optional values in the Go programming language:
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.
The Atlas Go SDK represents all arrays as pointers:
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.
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.
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.
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 dedicated methods for creating new models.
For example, instead of using the following:
GroupInvitationUpdateRequest{
...
}
Use the following dedicated method:
admin.NewGroupInvitationUpdateRequest(...)
Use golangci-lint to detect common errors when using the Atlas Go SDK. The Atlas Go SDK doesn't provide its own linter.
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.