github.com/weaviate/weaviate@v1.24.6/modules/text2vec-contextionary/client/version_checks.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package client
    13  
    14  import (
    15  	"fmt"
    16  	"regexp"
    17  	"strconv"
    18  )
    19  
    20  const (
    21  	inputVersionRegexString   = `^.*-v(?P<Major>[0-9]+)\.(?P<Minor>[0-9]+)\.(?P<Patch>[0-9]+)$`
    22  	minimumVersionRegexString = `^(?P<Major>[0-9]+)\.(?P<Minor>[0-9])+\.(?P<Patch>[0-9]+)$`
    23  )
    24  
    25  func extractVersionAndCompare(input, requiredMin string) (bool, error) {
    26  	inputRegexp := regexp.MustCompile(inputVersionRegexString)
    27  	minimumRegexp := regexp.MustCompile(minimumVersionRegexString)
    28  
    29  	if ok := inputRegexp.MatchString(input); !ok {
    30  		return false, fmt.Errorf("unexpected input version tag: %s", input)
    31  	}
    32  
    33  	if ok := minimumRegexp.MatchString(requiredMin); !ok {
    34  		return false, fmt.Errorf("unexpected threshold version tag: %s", requiredMin)
    35  	}
    36  
    37  	inputMatches := inputRegexp.FindAllStringSubmatch(input, 4)
    38  	inputMajor, _ := strconv.Atoi(inputMatches[0][1])
    39  	inputMinor, _ := strconv.Atoi(inputMatches[0][2])
    40  	inputPatch, _ := strconv.Atoi(inputMatches[0][3])
    41  
    42  	minimumMatches := minimumRegexp.FindAllStringSubmatch(requiredMin, 4)
    43  	minimumMajor, _ := strconv.Atoi(minimumMatches[0][1])
    44  	minimumMinor, _ := strconv.Atoi(minimumMatches[0][2])
    45  	minimumPatch, _ := strconv.Atoi(minimumMatches[0][3])
    46  
    47  	return compareSemver(inputMajor, inputMinor, inputPatch, minimumMajor, minimumMinor, minimumPatch), nil
    48  }
    49  
    50  func compareSemver(iMaj, iMin, iPat, rMaj, rMin, rPat int) bool {
    51  	if iMaj > rMaj {
    52  		return true
    53  	}
    54  
    55  	if iMaj < rMaj {
    56  		return false
    57  	}
    58  
    59  	if iMin > rMin {
    60  		return true
    61  	}
    62  
    63  	if iMin < rMin {
    64  		return false
    65  	}
    66  
    67  	if iPat > rPat {
    68  		return true
    69  	}
    70  
    71  	if iPat < rPat {
    72  		return false
    73  	}
    74  
    75  	return true
    76  }