Skip to content

Commit

Permalink
Parse versions string into struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mileusna committed Apr 14, 2023
1 parent 6b745d3 commit 004f562
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
37 changes: 16 additions & 21 deletions ua.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,19 @@ import (

// UserAgent struct containing all data extracted from parsed user-agent string
type UserAgent struct {
URL string
String string
Name string
Version string
OS string
OSVersion string
Device string
// VersionNo struct {
// Major int
// Minor int
// Patch int
// }
// OSVersionNo struct {
// Major int
// Minor int
// Patch int
// }
Mobile bool
Tablet bool
Desktop bool
Bot bool
VersionNo VersionNo
OSVersionNo VersionNo
URL string
String string
Name string
Version string
OS string
OSVersion string
Device string
Mobile bool
Tablet bool
Desktop bool
Bot bool
}

// Constants for browsers and operating systems for easier comparison
Expand Down Expand Up @@ -344,6 +336,9 @@ func Parse(userAgent string) UserAgent {
}
}

parseVersion(ua.Version, &ua.VersionNo)
parseVersion(ua.OSVersion, &ua.OSVersionNo)

return ua
}

Expand Down
2 changes: 1 addition & 1 deletion ua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestParse(t *testing.T) {
t.Error("\n", test[0], "Device should be", test[5], "not", ua.Device)
}

//fmt.Println(ua.OS, ua.OSVersion, ua.Device)
// fmt.Println(ua.Version, "==>", ua.VersionNoShort())

}
}
Expand Down
66 changes: 66 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package useragent

import (
"fmt"
"strconv"
"strings"
)

type VersionNo struct {
Major int
Minor int
Patch int
}

func parseVersion(ver string, verno *VersionNo) {
var err error
parts := strings.Split(ver, ".")
if len(parts) > 0 {
if verno.Major, err = strconv.Atoi(parts[0]); err != nil {
return
}
}
if len(parts) > 1 {
if verno.Minor, err = strconv.Atoi(parts[1]); err != nil {
return
}
if len(parts) > 2 {
if verno.Patch, err = strconv.Atoi(parts[2]); err != nil {
return
}
}
}
return
}

// VersionNoShort return version string in format <Major>.<Minor>
func (ua UserAgent) VersionNoShort() string {
if ua.VersionNo.Major == 0 && ua.VersionNo.Minor == 0 && ua.VersionNo.Patch == 0 {
return ""
}
return fmt.Sprintf("%d.%d", ua.VersionNo.Major, ua.VersionNo.Minor)
}

// VersionNoFull returns version string in format <Major>.<Minor>.<Patch>
func (ua UserAgent) VersionNoFull() string {
if ua.VersionNo.Major == 0 && ua.VersionNo.Minor == 0 && ua.VersionNo.Patch == 0 {
return ""
}
return fmt.Sprintf("%d.%d.%d", ua.VersionNo.Major, ua.VersionNo.Minor, ua.VersionNo.Patch)
}

// OSVersionNoShort returns OS version string in format <Major>.<Minor>
func (ua UserAgent) OSVersionNoShort() string {
if ua.OSVersionNo.Major == 0 && ua.OSVersionNo.Minor == 0 && ua.OSVersionNo.Patch == 0 {
return ""
}
return fmt.Sprintf("%d.%d", ua.OSVersionNo.Major, ua.OSVersionNo.Minor)
}

// OSVersionNoFull returns OS version string in format <Major>.<Minor>.<Patch>
func (ua UserAgent) OSVersionNoFull() string {
if ua.OSVersionNo.Major == 0 && ua.OSVersionNo.Minor == 0 && ua.OSVersionNo.Patch == 0 {
return ""
}
return fmt.Sprintf("%d.%d.%d", ua.OSVersionNo.Major, ua.OSVersionNo.Minor, ua.OSVersionNo.Patch)
}

0 comments on commit 004f562

Please sign in to comment.