Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace most helpers with a generic ToPointer function #489

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions fastly/acl_entries_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ func TestClient_BatchModifyACLEntries_Create(t *testing.T) {
Entries: []*BatchACLEntry{
{
Operation: CreateBatchOperation,
IP: String("127.0.0.1"),
Subnet: Int(24),
Negated: CBool(false),
Comment: String("ACL Entry 1"),
IP: ToPointer("127.0.0.1"),
Subnet: ToPointer(24),
Negated: ToPointer(Compatibool(false)),
Comment: ToPointer("ACL Entry 1"),
},
{
Operation: CreateBatchOperation,
IP: String("192.168.0.1"),
Subnet: Int(24),
Negated: CBool(true),
Comment: String("ACL Entry 2"),
IP: ToPointer("192.168.0.1"),
Subnet: ToPointer(24),
Negated: ToPointer(Compatibool(true)),
Comment: ToPointer("ACL Entry 2"),
},
},
}
Expand Down Expand Up @@ -120,17 +120,17 @@ func TestClient_BatchModifyACLEntries_Delete(t *testing.T) {
Entries: []*BatchACLEntry{
{
Operation: CreateBatchOperation,
IP: String("127.0.0.1"),
Subnet: Int(24),
Negated: CBool(false),
Comment: String("ACL Entry 1"),
IP: ToPointer("127.0.0.1"),
Subnet: ToPointer(24),
Negated: ToPointer(Compatibool(false)),
Comment: ToPointer("ACL Entry 1"),
},
{
Operation: CreateBatchOperation,
IP: String("192.168.0.1"),
Subnet: Int(24),
Negated: CBool(true),
Comment: String("ACL Entry 2"),
IP: ToPointer("192.168.0.1"),
Subnet: ToPointer(24),
Negated: ToPointer(Compatibool(true)),
Comment: ToPointer("ACL Entry 2"),
},
},
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestClient_BatchModifyACLEntries_Delete(t *testing.T) {
Entries: []*BatchACLEntry{
{
Operation: DeleteBatchOperation,
ID: String(createdACLEntries[0].ID),
ID: ToPointer(createdACLEntries[0].ID),
},
},
}
Expand Down Expand Up @@ -219,17 +219,17 @@ func TestClient_BatchModifyACLEntries_Update(t *testing.T) {
Entries: []*BatchACLEntry{
{
Operation: CreateBatchOperation,
IP: String("127.0.0.1"),
Subnet: Int(24),
Negated: CBool(false),
Comment: String("ACL Entry 1"),
IP: ToPointer("127.0.0.1"),
Subnet: ToPointer(24),
Negated: ToPointer(Compatibool(false)),
Comment: ToPointer("ACL Entry 1"),
},
{
Operation: CreateBatchOperation,
IP: String("192.168.0.1"),
Subnet: Int(24),
Negated: CBool(true),
Comment: String("ACL Entry 2"),
IP: ToPointer("192.168.0.1"),
Subnet: ToPointer(24),
Negated: ToPointer(Compatibool(true)),
Comment: ToPointer("ACL Entry 2"),
},
},
}
Expand Down Expand Up @@ -264,11 +264,11 @@ func TestClient_BatchModifyACLEntries_Update(t *testing.T) {
Entries: []*BatchACLEntry{
{
Operation: UpdateBatchOperation,
ID: String(createdACLEntries[0].ID),
IP: String("127.0.0.2"),
Subnet: Int(16),
Negated: CBool(true),
Comment: String("Updated ACL Entry 1"),
ID: ToPointer(createdACLEntries[0].ID),
IP: ToPointer("127.0.0.2"),
Subnet: ToPointer(16),
Negated: ToPointer(Compatibool(true)),
Comment: ToPointer("Updated ACL Entry 1"),
},
},
}
Expand Down
12 changes: 6 additions & 6 deletions fastly/acl_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func TestClient_ACLEntries(t *testing.T) {
e, err = c.CreateACLEntry(&CreateACLEntryInput{
ServiceID: testService.ID,
ACLID: testACL.ID,
IP: String("10.0.0.3"),
Subnet: Int(8),
Negated: CBool(false),
Comment: String("test entry"),
IP: ToPointer("10.0.0.3"),
Subnet: ToPointer(8),
Negated: ToPointer(Compatibool(false)),
Comment: ToPointer("test entry"),
})
})
if err != nil {
Expand Down Expand Up @@ -124,8 +124,8 @@ func TestClient_ACLEntries(t *testing.T) {
ServiceID: testService.ID,
ACLID: testACL.ID,
ID: e.ID,
IP: String("10.0.0.4"),
Negated: CBool(true),
IP: ToPointer("10.0.0.4"),
Negated: ToPointer(Compatibool(true)),
})
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions fastly/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestClient_ACLs(t *testing.T) {
a, err = c.CreateACL(&CreateACLInput{
ServiceID: testServiceID,
ServiceVersion: testVersion.Number,
Name: String("test_acl"),
Name: ToPointer("test_acl"),
})
})
if err != nil {
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestClient_ACLs(t *testing.T) {
ServiceID: testServiceID,
ServiceVersion: testVersion.Number,
Name: "test_acl",
NewName: String("new_test_acl"),
NewName: ToPointer("new_test_acl"),
})
})
if err != nil {
Expand Down
32 changes: 16 additions & 16 deletions fastly/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func TestClient_Backends(t *testing.T) {
b, err = c.CreateBackend(&CreateBackendInput{
ServiceID: testServiceID,
ServiceVersion: tv.Number,
Name: String("test-backend"),
Address: String("integ-test.go-fastly.com"),
ConnectTimeout: Int(1500),
OverrideHost: String("origin.example.com"),
SSLCheckCert: CBool(false),
SSLCiphers: String("DHE-RSA-AES256-SHA:DHE-RSA-CAMELLIA256-SHA:AES256-GCM-SHA384"),
SSLSNIHostname: String("ssl-hostname.com"),
Name: ToPointer("test-backend"),
Address: ToPointer("integ-test.go-fastly.com"),
ConnectTimeout: ToPointer(1500),
OverrideHost: ToPointer("origin.example.com"),
SSLCheckCert: ToPointer(Compatibool(false)),
SSLCiphers: ToPointer("DHE-RSA-AES256-SHA:DHE-RSA-CAMELLIA256-SHA:AES256-GCM-SHA384"),
SSLSNIHostname: ToPointer("ssl-hostname.com"),
})
})
if err != nil {
Expand Down Expand Up @@ -124,13 +124,13 @@ func TestClient_Backends(t *testing.T) {
ServiceID: testServiceID,
ServiceVersion: tv.Number,
Name: "test-backend",
NewName: String("new-test-backend"),
OverrideHost: String("www.example.com"),
Port: Int(1234),
ShareKey: String("shared-key"),
SSLCiphers: String("RC4:!COMPLEMENTOFDEFAULT"),
SSLCheckCert: CBool(false),
SSLSNIHostname: String("ssl-hostname-updated.com"),
NewName: ToPointer("new-test-backend"),
OverrideHost: ToPointer("www.example.com"),
Port: ToPointer(1234),
ShareKey: ToPointer("shared-key"),
SSLCiphers: ToPointer("RC4:!COMPLEMENTOFDEFAULT"),
SSLCheckCert: ToPointer(Compatibool(false)),
SSLSNIHostname: ToPointer("ssl-hostname-updated.com"),
})
})
if err != nil {
Expand Down Expand Up @@ -183,8 +183,8 @@ func TestClient_Backends(t *testing.T) {
ServiceID: testServiceID,
ServiceVersion: tv.Number,
Name: "new-test-backend",
OverrideHost: String(""),
Port: Int(0),
OverrideHost: ToPointer(""),
Port: ToPointer(0),
})
})
if err != nil {
Expand Down
30 changes: 2 additions & 28 deletions fastly/basictypes_helper.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
package fastly

// String is a helper that returns a pointer to the string value passed in.
func String(v string) *string {
return &v
}

// Int is a helper that returns a pointer to the int value passed in.
func Int(v int) *int {
return &v
}

// Uint is a helper that returns a pointer to the uint value passed in.
func Uint(v uint) *uint {
return &v
}

// Uint8 is a helper that returns a pointer to the uint8 value passed in.
func Uint8(v uint8) *uint8 {
return &v
}

// Bool is a helper that returns a pointer to the bool value passed in.
func Bool(v bool) *bool {
// ToPointer converts T to *T.
func ToPointer[T string | int | uint | uint8 | bool | CacheSettingAction | Compatibool | ERLAction | ERLLogger | ERLWindowSize | RequestSettingAction | RequestSettingXFF | S3AccessControlList | S3Redundancy | S3ServerSideEncryption](v T) *T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why you specify all these types rather than using any?

I wonder also if you could use the ~ sigil on the primitive types to get the underlying types? If not any then ~string | ~int | ~uint | ~uint8 | ~bool?

Copy link
Member

@joeshaw joeshaw Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha never mind, I just saw the follow-up PR. (#490)

return &v
}

Expand All @@ -36,9 +16,3 @@ func NullString(v string) *string {
}
return &v
}

// CBool is a helper that returns a pointer to a Compatibool with the value passed in.
func CBool(b bool) *Compatibool {
c := Compatibool(b)
return &c
}
24 changes: 12 additions & 12 deletions fastly/bigquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func TestClient_Bigqueries(t *testing.T) {
bq, err = c.CreateBigQuery(&CreateBigQueryInput{
ServiceID: testServiceID,
ServiceVersion: tv.Number,
Name: String("test-bigquery"),
ProjectID: String("example-fastly-log"),
Dataset: String("fastly_log_test"),
Table: String("fastly_logs"),
Template: String(""),
User: String("[email protected]"),
AccountName: String("service-account"),
SecretKey: String(secretKey),
Format: String("{\n \"timestamp\":\"%{begin:%Y-%m-%dT%H:%M:%S}t\",\n \"time_elapsed\":%{time.elapsed.usec}V,\n \"is_tls\":%{if(req.is_ssl, \"true\", \"false\")}V,\n \"client_ip\":\"%{req.http.Fastly-Client-IP}V\",\n \"geo_city\":\"%{client.geo.city}V\",\n \"geo_country_code\":\"%{client.geo.country_code}V\",\n \"request\":\"%{req.request}V\",\n \"host\":\"%{req.http.Fastly-Orig-Host}V\",\n \"url\":\"%{json.escape(req.url)}V\",\n \"request_referer\":\"%{json.escape(req.http.Referer)}V\",\n \"request_user_agent\":\"%{json.escape(req.http.User-Agent)}V\",\n \"request_accept_language\":\"%{json.escape(req.http.Accept-Language)}V\",\n \"request_accept_charset\":\"%{json.escape(req.http.Accept-Charset)}V\",\n \"cache_status\":\"%{regsub(fastly_info.state, \"^(HIT-(SYNTH)|(HITPASS|HIT|MISS|PASS|ERROR|PIPE)).*\", \"\\\\2\\\\3\") }V\"\n}"),
Placement: String("waf_debug"),
FormatVersion: Int(2),
Name: ToPointer("test-bigquery"),
ProjectID: ToPointer("example-fastly-log"),
Dataset: ToPointer("fastly_log_test"),
Table: ToPointer("fastly_logs"),
Template: ToPointer(""),
User: ToPointer("[email protected]"),
AccountName: ToPointer("service-account"),
SecretKey: ToPointer(secretKey),
Format: ToPointer("{\n \"timestamp\":\"%{begin:%Y-%m-%dT%H:%M:%S}t\",\n \"time_elapsed\":%{time.elapsed.usec}V,\n \"is_tls\":%{if(req.is_ssl, \"true\", \"false\")}V,\n \"client_ip\":\"%{req.http.Fastly-Client-IP}V\",\n \"geo_city\":\"%{client.geo.city}V\",\n \"geo_country_code\":\"%{client.geo.country_code}V\",\n \"request\":\"%{req.request}V\",\n \"host\":\"%{req.http.Fastly-Orig-Host}V\",\n \"url\":\"%{json.escape(req.url)}V\",\n \"request_referer\":\"%{json.escape(req.http.Referer)}V\",\n \"request_user_agent\":\"%{json.escape(req.http.User-Agent)}V\",\n \"request_accept_language\":\"%{json.escape(req.http.Accept-Language)}V\",\n \"request_accept_charset\":\"%{json.escape(req.http.Accept-Charset)}V\",\n \"cache_status\":\"%{regsub(fastly_info.state, \"^(HIT-(SYNTH)|(HITPASS|HIT|MISS|PASS|ERROR|PIPE)).*\", \"\\\\2\\\\3\") }V\"\n}"),
Placement: ToPointer("waf_debug"),
FormatVersion: ToPointer(2),
})
})
if err != nil {
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestClient_Bigqueries(t *testing.T) {
ServiceID: testServiceID,
ServiceVersion: tv.Number,
Name: "test-bigquery",
NewName: String("new-test-bigquery"),
NewName: ToPointer("new-test-bigquery"),
})
})
if err != nil {
Expand Down
Loading
Loading