github.com/prebid/prebid-server/v2@v2.18.0/util/iosutil/iosutil.go (about)

     1  package iosutil
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // Version specifies the version of an iOS device.
    10  type Version struct {
    11  	Major int
    12  	Minor int
    13  }
    14  
    15  // ParseVersion parses the major.minor version for an iOS device.
    16  func ParseVersion(v string) (Version, error) {
    17  	parts := strings.Split(v, ".")
    18  
    19  	if len(parts) < 2 || len(parts) > 3 {
    20  		return Version{}, errors.New("expected either major.minor or major.minor.patch format")
    21  	}
    22  
    23  	major, err := strconv.Atoi(parts[0])
    24  	if err != nil {
    25  		return Version{}, errors.New("major version is not an integer")
    26  	}
    27  
    28  	minor, err := strconv.Atoi(parts[1])
    29  	if err != nil {
    30  		return Version{}, errors.New("minor version is not an integer")
    31  	}
    32  
    33  	version := Version{
    34  		Major: major,
    35  		Minor: minor,
    36  	}
    37  	return version, nil
    38  }
    39  
    40  // Equal returns true if the iOS device version is equal to the desired major and minor version, using semantic versioning.
    41  func (v Version) Equal(major, minor int) bool {
    42  	if v.Major == major {
    43  		return v.Minor == minor
    44  	}
    45  
    46  	return false
    47  }
    48  
    49  // EqualOrGreater returns true if the iOS device version is equal or greater to the desired version, using semantic versioning.
    50  func (v Version) EqualOrGreater(major, minor int) bool {
    51  	if v.Major == major {
    52  		return v.Minor >= minor
    53  	}
    54  
    55  	return v.Major > major
    56  }
    57  
    58  // VersionClassification describes iOS version classifications which are important to Prebid Server.
    59  type VersionClassification int
    60  
    61  // Values of VersionClassification.
    62  const (
    63  	VersionUnknown VersionClassification = iota
    64  	Version140
    65  	Version141
    66  	Version142OrGreater
    67  )
    68  
    69  // DetectVersionClassification detects the iOS version classification.
    70  func DetectVersionClassification(v string) VersionClassification {
    71  	// semantic versioning comparison second. parsing required.
    72  	if iosVersion, err := ParseVersion(v); err == nil {
    73  		if iosVersion.Equal(14, 0) {
    74  			return Version140
    75  		}
    76  		if iosVersion.Equal(14, 1) {
    77  			return Version141
    78  		}
    79  		if iosVersion.EqualOrGreater(14, 2) {
    80  			return Version142OrGreater
    81  		}
    82  	}
    83  	return VersionUnknown
    84  }