From b5102bde56356140920b9bfdb1147445aee0e68e Mon Sep 17 00:00:00 2001 From: Sebastian Burmester Date: Fri, 23 Aug 2024 13:06:18 +0200 Subject: [PATCH] removes pointer of pointer and dereferencing the pointer of pointer (#4) --- force/JobTypes.go | 2 +- force/force.go | 16 +++++++--------- force/force_test.go | 4 ++-- force/job.go | 12 ++++++------ force/job_test.go | 6 +++--- force/limits_test.go | 2 +- force/oauth_test.go | 2 +- force/query_test.go | 8 ++++---- force/sobjects_test.go | 24 ++++++++++++------------ 9 files changed, 37 insertions(+), 39 deletions(-) diff --git a/force/JobTypes.go b/force/JobTypes.go index b49aa58..8c00534 100644 --- a/force/JobTypes.go +++ b/force/JobTypes.go @@ -17,7 +17,7 @@ type ObjectMapper func(objects any) [][]string type Job struct { info *JobInfo operation JobOperation - forceApi *ForceApiInterface + forceApi ForceApiInterface objectMapper ObjectMapper client BulkClient apiVersion string diff --git a/force/force.go b/force/force.go index 2b2109f..5637180 100644 --- a/force/force.go +++ b/force/force.go @@ -80,7 +80,7 @@ func WithRefreshToken(clientId, clientSecret, refreshToken string) APIConfig { } } -func NewClient(cfg ...APIConfig) (*ForceApiInterface, error) { +func NewClient(cfg ...APIConfig) (ForceApiInterface, error) { f := &ForceApi{ apiResources: make(map[string]string), apiSObjects: make(map[string]*SObjectMetaData), @@ -127,18 +127,17 @@ func NewClient(cfg ...APIConfig) (*ForceApiInterface, error) { return nil, err } - var fapi ForceApiInterface = f - return &fapi, nil + return f, nil } -func Create(version, clientId, clientSecret, userName, password, securityToken, environment string) (*ForceApiInterface, error) { +func Create(version, clientId, clientSecret, userName, password, securityToken, environment string) (ForceApiInterface, error) { return NewClient( WithOAuth(version, clientId, clientSecret, userName, password, securityToken, environment), WithClient(http.DefaultClient), ) } -func CreateWithAccessToken(version, clientId, accessToken, instanceUrl string, httpClient *http.Client) (*ForceApiInterface, error) { +func CreateWithAccessToken(version, clientId, accessToken, instanceUrl string, httpClient *http.Client) (ForceApiInterface, error) { return NewClient( WithAccessToken(clientId, accessToken, instanceUrl), WithClient(httpClient), @@ -146,7 +145,7 @@ func CreateWithAccessToken(version, clientId, accessToken, instanceUrl string, h } // TODO: This likely never has worked because the refresh token passed in forceApi.RefreshToken() is always an empty string? -func CreateWithRefreshToken(version, clientId, accessToken, instanceUrl string) (*ForceApiInterface, error) { +func CreateWithRefreshToken(version, clientId, accessToken, instanceUrl string) (ForceApiInterface, error) { oauth := &ForceOauth{ clientId: clientId, AccessToken: accessToken, @@ -181,12 +180,11 @@ func CreateWithRefreshToken(version, clientId, accessToken, instanceUrl string) return nil, err } - var fapi ForceApiInterface = forceApi - return &fapi, nil + return forceApi, nil } // Used when running tests. -func createTest() *ForceApiInterface { +func createTest() ForceApiInterface { forceApi, err := Create(testVersion, testClientId, testClientSecret, testUserName, testPassword, testSecurityToken, testEnvironment) if err != nil { fmt.Printf("Unable to create ForceApi for test: %v", err) diff --git a/force/force_test.go b/force/force_test.go index d219def..a09bc32 100644 --- a/force/force_test.go +++ b/force/force_test.go @@ -39,12 +39,12 @@ func TestCreateWithAccessToken(t *testing.T) { if err != nil { t.Fatalf("Unable to create new force api instance using pre-defined oauth details: %#v", err) } - if err := (*newForceApi).GetOauth().Validate(); err != nil { + if err := newForceApi.GetOauth().Validate(); err != nil { t.Fatalf("Oauth object is invlaid: %#v", err) } // We should be able to make a basic query now with the newly created object (i.e. the oauth details should be correctly usable). - _, err = (*newForceApi).DescribeSObject(&sobjects.Account{}) + _, err = newForceApi.DescribeSObject(&sobjects.Account{}) if err != nil { t.Fatalf("Failed to retrieve description of sobject: %v", err) } diff --git a/force/job.go b/force/job.go index be6499d..32407c4 100644 --- a/force/job.go +++ b/force/job.go @@ -11,7 +11,7 @@ import ( ) // CreateJob creates a new pointer to an instance of Job. Can be Modified with the given JobOptionsFuncs -func CreateJob(fapi *ForceApiInterface, opts ...OptionsFunc) *Job { +func CreateJob(fapi ForceApiInterface, opts ...OptionsFunc) *Job { job := &Job{ forceApi: fapi, operation: JobOperation{}, @@ -68,7 +68,7 @@ func (job *Job) Start() error { "operation": job.operation.Operation, } - if err := (*job.forceApi).Post("/services/data/"+job.apiVersion+"/jobs/ingest", nil, params, job.info); err != nil { + if err := job.forceApi.Post("/services/data/"+job.apiVersion+"/jobs/ingest", nil, params, job.info); err != nil { return err } job.operation.ProgressReporter("job created") @@ -88,7 +88,7 @@ func (job *Job) Run(payload any) error { } urlFormat := "%s%s" - instanceUrl := (*job.forceApi).GetInstanceURL() + instanceUrl := job.forceApi.GetInstanceURL() contentUrl := job.info.ContentURL if !strings.HasPrefix(contentUrl, "/") && !strings.HasSuffix(instanceUrl, "/") { urlFormat = "%s/%s" @@ -100,7 +100,7 @@ func (job *Job) Run(payload any) error { } req.Header.Set("Content-Type", "text/csv") - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", (*job.forceApi).GetAccessToken())) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", job.forceApi.GetAccessToken())) res, err := job.client.Do(req) if err != nil { @@ -122,7 +122,7 @@ func (job *Job) Run(payload any) error { "state": "UploadComplete", } - if err := (*job.forceApi).Patch(statusURI, nil, params, job.info); err != nil { + if err := job.forceApi.Patch(statusURI, nil, params, job.info); err != nil { return err } @@ -144,7 +144,7 @@ func (job *Job) marshalCSV(payload any) (io.Reader, error) { return bytes.NewReader(bulkData.Bytes()), nil } -func (job *Job) GetForceApi() *ForceApiInterface { +func (job *Job) GetForceApi() ForceApiInterface { return job.forceApi } diff --git a/force/job_test.go b/force/job_test.go index 94820dd..568386e 100644 --- a/force/job_test.go +++ b/force/job_test.go @@ -35,7 +35,7 @@ func TestCheckJobStatus(t *testing.T) { t.Fatalf("Could not run the job: %v ", err) } - _, err = (*fapi).CheckJobStatus(ops, 3) + _, err = fapi.CheckJobStatus(ops, 3) if err != nil { deleteSObject(fapi, t, accObj.Id) @@ -58,7 +58,7 @@ func objMapper(objects any) [][]string { return records } -func insertSAccount(forceApi *ForceApiInterface, t *testing.T) *sobjects.Account { +func insertSAccount(forceApi ForceApiInterface, t *testing.T) *sobjects.Account { // Need some random text for name field. someText := randomString(10) @@ -66,7 +66,7 @@ func insertSAccount(forceApi *ForceApiInterface, t *testing.T) *sobjects.Account acc := &sobjects.Account{} acc.Name = someText - resp, err := (*forceApi).InsertSObject(acc) + resp, err := forceApi.InsertSObject(acc) if err != nil { t.Fatalf("Insert SObject Account failed: %v", err) } diff --git a/force/limits_test.go b/force/limits_test.go index 0b9157f..1f4d9d3 100644 --- a/force/limits_test.go +++ b/force/limits_test.go @@ -6,7 +6,7 @@ import ( func TestLimits(t *testing.T) { forceApi := createTest() - limits, err := (*forceApi).GetLimits() + limits, err := forceApi.GetLimits() if err != nil { // Developer Accounts, which the testbed uses, do not have access to the limits API. So this will always fail. // t.Fatalf("Failed to get Limits: %v", err) diff --git a/force/oauth_test.go b/force/oauth_test.go index 8334efc..7b32ee0 100644 --- a/force/oauth_test.go +++ b/force/oauth_test.go @@ -7,7 +7,7 @@ import ( func TestOauth(t *testing.T) { forceApi := createTest() // Verify oauth object is valid - if err := (*forceApi).GetOauth().Validate(); err != nil { + if err := forceApi.GetOauth().Validate(); err != nil { t.Fatalf("Oauth object is invlaid: %#v", err) } } diff --git a/force/query_test.go b/force/query_test.go index 728da7b..d8b1f3a 100644 --- a/force/query_test.go +++ b/force/query_test.go @@ -18,13 +18,13 @@ type AccountQueryResponse struct { func TestQuery(t *testing.T) { forceApi := createTest() - desc, err := (*forceApi).DescribeSObject(&sobjects.Account{}) + desc, err := forceApi.DescribeSObject(&sobjects.Account{}) if err != nil { t.Fatalf("Failed to retrieve description of sobject: %v", err) } list := &AccountQueryResponse{} - err = (*forceApi).Query(BuildQuery(desc.AllFields, desc.Name, nil), list) + err = forceApi.Query(BuildQuery(desc.AllFields, desc.Name, nil), list) if err != nil { t.Fatalf("Failed to query: %v", err) } @@ -39,13 +39,13 @@ func TestQueryAll(t *testing.T) { deleteSObject(forceApi, t, newId) // Then look for it. - desc, err := (*forceApi).DescribeSObject(&sobjects.Account{}) + desc, err := forceApi.DescribeSObject(&sobjects.Account{}) if err != nil { t.Fatalf("Failed to retrieve description of sobject: %v", err) } list := &AccountQueryResponse{} - err = (*forceApi).QueryAll(fmt.Sprintf(queryAll, desc.AllFields, newId), list) + err = forceApi.QueryAll(fmt.Sprintf(queryAll, desc.AllFields, newId), list) if err != nil { t.Fatalf("Failed to queryAll: %v", err) } diff --git a/force/sobjects_test.go b/force/sobjects_test.go index 7cc2161..7253f38 100644 --- a/force/sobjects_test.go +++ b/force/sobjects_test.go @@ -23,7 +23,7 @@ func (t *CustomSObject) ApiName() string { func TestDescribeSobjects(t *testing.T) { forceApi := createTest() - objects, err := (*forceApi).DescribeSObjects() + objects, err := forceApi.DescribeSObjects() if err != nil { t.Fatal("Failed to retrieve SObjects", err) } @@ -34,7 +34,7 @@ func TestDescribeSObject(t *testing.T) { forceApi := createTest() acc := &sobjects.Account{} - desc, err := (*forceApi).DescribeSObject(acc) + desc, err := forceApi.DescribeSObject(acc) if err != nil { t.Fatalf("Cannot retrieve SObject Description for Account SObject: %v", err) } @@ -47,7 +47,7 @@ func TestGetSObject(t *testing.T) { // Test Standard Object acc := &sobjects.Account{} - err := (*forceApi).GetSObject(AccountId, nil, acc) + err := forceApi.GetSObject(AccountId, nil, acc) if err != nil { t.Fatalf("Cannot retrieve SObject Account: %v", err) } @@ -57,7 +57,7 @@ func TestGetSObject(t *testing.T) { // Test Custom Object customObject := &CustomSObject{} - err = (*forceApi).GetSObject(CustomObjectId, nil, customObject) + err = forceApi.GetSObject(CustomObjectId, nil, customObject) if err != nil { t.Fatalf("Cannot retrieve SObject CustomObject: %v", err) } @@ -69,7 +69,7 @@ func TestGetSObject(t *testing.T) { accFields := &sobjects.Account{} - err = (*forceApi).GetSObject(AccountId, fields, accFields) + err = forceApi.GetSObject(AccountId, fields, accFields) if err != nil { t.Fatalf("Cannot retrieve SObject Account fields: %v", err) } @@ -86,13 +86,13 @@ func TestUpdateSObject(t *testing.T) { acc := &sobjects.Account{} acc.Name = someText - err := (*forceApi).UpdateSObject(AccountId, acc) + err := forceApi.UpdateSObject(AccountId, acc) if err != nil { t.Fatalf("Cannot update SObject Account: %v", err) } // Read back and verify - err = (*forceApi).GetSObject(AccountId, nil, acc) + err = forceApi.GetSObject(AccountId, nil, acc) if err != nil { t.Fatalf("Cannot retrieve SObject Account: %v", err) } @@ -110,7 +110,7 @@ func TestInsertDeleteSObject(t *testing.T) { deleteSObject(fapi, t, objectId) } -func insertSObject(forceApi *ForceApiInterface, t *testing.T) string { +func insertSObject(forceApi ForceApiInterface, t *testing.T) string { // Need some random text for name field. someText := randomString(10) @@ -118,7 +118,7 @@ func insertSObject(forceApi *ForceApiInterface, t *testing.T) string { acc := &sobjects.Account{} acc.Name = someText - resp, err := (*forceApi).InsertSObject(acc) + resp, err := forceApi.InsertSObject(acc) if err != nil { t.Fatalf("Insert SObject Account failed: %v", err) } @@ -130,17 +130,17 @@ func insertSObject(forceApi *ForceApiInterface, t *testing.T) string { return resp.Id } -func deleteSObject(fapi *ForceApiInterface, t *testing.T, id string) { +func deleteSObject(fapi ForceApiInterface, t *testing.T, id string) { // Test Standard Object acc := &sobjects.Account{} - err := (*fapi).DeleteSObject(id, acc) + err := fapi.DeleteSObject(id, acc) if err != nil { t.Fatalf("Delete SObject Account failed: %v", err) } // Read back and verify - err = (*fapi).GetSObject(id, nil, acc) + err = fapi.GetSObject(id, nil, acc) if err == nil { t.Fatalf("Delete SObject Account failed, was able to retrieve deleted object: %+v", acc) }