Skip to content

Commit

Permalink
removes pointer of pointer and dereferencing the pointer of pointer (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
sBurmester authored Aug 23, 2024
1 parent dc74466 commit b5102bd
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 39 deletions.
2 changes: 1 addition & 1 deletion force/JobTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions force/force.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -127,26 +127,25 @@ 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),
)
}

// 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,
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions force/force_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions force/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down Expand Up @@ -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")
Expand All @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions force/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -58,15 +58,15 @@ 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)

// Test Standard Object
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)
}
Expand Down
2 changes: 1 addition & 1 deletion force/limits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion force/oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
8 changes: 4 additions & 4 deletions force/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
24 changes: 12 additions & 12 deletions force/sobjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -110,15 +110,15 @@ 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)

// Test Standard Object
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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit b5102bd

Please sign in to comment.