Skip to content

Commit

Permalink
Merge pull request #120 from Skee/main
Browse files Browse the repository at this point in the history
add jsutil.MaybeInt wrapper; apply it to IncomingProperties.Asn
  • Loading branch information
syumai authored Jul 30, 2024
2 parents e5ed933 + 0b2343b commit 50d520d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cloudflare/fetch/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func NewIncomingProperties(ctx context.Context) (*IncomingProperties, error) {
Latitude: jsutil.MaybeString(cf.Get("latitude")),
TLSCipher: jsutil.MaybeString(cf.Get("tlsCipher")),
Continent: jsutil.MaybeString(cf.Get("continent")),
Asn: cf.Get("asn").Int(),
Asn: jsutil.MaybeInt(cf.Get("asn")),
ClientAcceptEncoding: jsutil.MaybeString(cf.Get("clientAcceptEncoding")),
Country: jsutil.MaybeString(cf.Get("country")),
TLSClientAuth: NewIncomingTLSClientAuth(cf.Get("tlsClientAuth")),
Expand Down
8 changes: 8 additions & 0 deletions internal/jsutil/jsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ func MaybeString(v js.Value) string {
return v.String()
}

// MaybeInt returns int value of given JavaScript value or returns nil if the value is undefined.
func MaybeInt(v js.Value) int {
if v.IsUndefined() {
return 0
}
return v.Int()
}

// MaybeDate returns time.Time value of given JavaScript Date value or returns nil if the value is undefined.
func MaybeDate(v js.Value) (time.Time, error) {
if v.IsUndefined() {
Expand Down

0 comments on commit 50d520d

Please sign in to comment.