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

add jsutil.MaybeInt wrapper; apply it to IncomingProperties.Asn #120

Merged
merged 1 commit into from
Jul 30, 2024
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
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
Loading