Skip to content

Commit 50d520d

Browse files
authored
Merge pull request #120 from Skee/main
add jsutil.MaybeInt wrapper; apply it to IncomingProperties.Asn
2 parents e5ed933 + 0b2343b commit 50d520d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

cloudflare/fetch/property.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func NewIncomingProperties(ctx context.Context) (*IncomingProperties, error) {
184184
Latitude: jsutil.MaybeString(cf.Get("latitude")),
185185
TLSCipher: jsutil.MaybeString(cf.Get("tlsCipher")),
186186
Continent: jsutil.MaybeString(cf.Get("continent")),
187-
Asn: cf.Get("asn").Int(),
187+
Asn: jsutil.MaybeInt(cf.Get("asn")),
188188
ClientAcceptEncoding: jsutil.MaybeString(cf.Get("clientAcceptEncoding")),
189189
Country: jsutil.MaybeString(cf.Get("country")),
190190
TLSClientAuth: NewIncomingTLSClientAuth(cf.Get("tlsClientAuth")),

internal/jsutil/jsutil.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ func MaybeString(v js.Value) string {
9797
return v.String()
9898
}
9999

100+
// MaybeInt returns int value of given JavaScript value or returns nil if the value is undefined.
101+
func MaybeInt(v js.Value) int {
102+
if v.IsUndefined() {
103+
return 0
104+
}
105+
return v.Int()
106+
}
107+
100108
// MaybeDate returns time.Time value of given JavaScript Date value or returns nil if the value is undefined.
101109
func MaybeDate(v js.Value) (time.Time, error) {
102110
if v.IsUndefined() {

0 commit comments

Comments
 (0)