gitlab.com/evatix-go/core@v1.3.55/coreversion/Version.go (about)

     1  package coreversion
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  	"gitlab.com/evatix-go/core/corecmp"
     8  	"gitlab.com/evatix-go/core/corecomparator"
     9  	"gitlab.com/evatix-go/core/coredata/corejson"
    10  	"gitlab.com/evatix-go/core/enums/versionindexes"
    11  )
    12  
    13  type Version struct {
    14  	VersionCompact string // ex : 1.0.1
    15  	VersionMajor   int
    16  	VersionMinor   int
    17  	VersionPatch   int
    18  	VersionBuild   int
    19  }
    20  
    21  func (it *Version) String() string {
    22  	return it.VersionDisplay()
    23  }
    24  
    25  func (it *Version) VersionDisplay() string {
    26  	if it == nil || it.VersionCompact == "" {
    27  		return constants.EmptyString
    28  	}
    29  
    30  	return VSymbol + it.VersionCompact
    31  }
    32  
    33  func (it *Version) VersionDisplayMajor() string {
    34  	if it == nil || it.VersionCompact == "" || it.IsMajorInvalid() {
    35  		return constants.EmptyString
    36  	}
    37  
    38  	return VSymbol + strconv.Itoa(it.VersionMajor)
    39  }
    40  
    41  func (it *Version) VersionDisplayMajorMinor() string {
    42  	if it.IsMinorInvalid() {
    43  		return it.VersionDisplayMajor()
    44  	}
    45  
    46  	return VSymbol +
    47  		strconv.Itoa(it.VersionMajor) +
    48  		constants.Dot +
    49  		strconv.Itoa(it.VersionMinor)
    50  }
    51  
    52  func (it *Version) VersionDisplayMajorMinorPatch() string {
    53  	if it.IsPatchInvalid() {
    54  		return it.VersionDisplayMajorMinor()
    55  	}
    56  
    57  	return VSymbol +
    58  		strconv.Itoa(it.VersionMajor) +
    59  		constants.Dot +
    60  		strconv.Itoa(it.VersionMinor) +
    61  		constants.Dot +
    62  		strconv.Itoa(it.VersionPatch)
    63  }
    64  
    65  func (it *Version) MajorString() string {
    66  	if it == nil {
    67  		return constants.EmptyString
    68  	}
    69  
    70  	return strconv.Itoa(it.VersionMajor)
    71  }
    72  
    73  func (it *Version) MinorString() string {
    74  	if it == nil {
    75  		return constants.EmptyString
    76  	}
    77  
    78  	return strconv.Itoa(it.VersionMinor)
    79  }
    80  
    81  func (it *Version) PatchString() string {
    82  	if it == nil {
    83  		return constants.EmptyString
    84  	}
    85  
    86  	return strconv.Itoa(it.VersionPatch)
    87  }
    88  
    89  func (it *Version) BuildString() string {
    90  	if it == nil {
    91  		return constants.EmptyString
    92  	}
    93  
    94  	return strconv.Itoa(it.VersionBuild)
    95  }
    96  
    97  func (it *Version) HasMajor() bool {
    98  	return it != nil && it.VersionMajor > InvalidVersionValue
    99  }
   100  
   101  func (it *Version) HasMinor() bool {
   102  	return it != nil && it.VersionMinor > InvalidVersionValue
   103  }
   104  
   105  func (it *Version) HasPatch() bool {
   106  	return it != nil && it.VersionPatch > InvalidVersionValue
   107  }
   108  
   109  func (it *Version) HasBuild() bool {
   110  	return it != nil && it.VersionBuild > InvalidVersionValue
   111  }
   112  
   113  func (it *Version) IsMajorInvalid() bool {
   114  	return it == nil || it.VersionMajor == InvalidVersionValue
   115  }
   116  
   117  func (it *Version) IsMinorInvalid() bool {
   118  	return it == nil || it.VersionMinor == InvalidVersionValue
   119  }
   120  
   121  func (it *Version) IsPatchInvalid() bool {
   122  	return it == nil || it.VersionPatch == InvalidVersionValue
   123  }
   124  
   125  func (it *Version) IsBuildInvalid() bool {
   126  	return it == nil || it.VersionBuild == InvalidVersionValue
   127  }
   128  
   129  func (it *Version) IsMajorInvalidOrZero() bool {
   130  	return it == nil ||
   131  		it.VersionMajor == InvalidVersionValue ||
   132  		it.VersionMajor == constants.Zero
   133  }
   134  
   135  func (it *Version) IsMinorInvalidOrZero() bool {
   136  	return it == nil ||
   137  		it.VersionMinor == InvalidVersionValue ||
   138  		it.VersionMinor == constants.Zero
   139  }
   140  
   141  func (it *Version) IsPatchInvalidOrZero() bool {
   142  	return it == nil ||
   143  		it.VersionPatch == InvalidVersionValue ||
   144  		it.VersionPatch == constants.Zero
   145  }
   146  
   147  func (it *Version) IsBuildInvalidOrZero() bool {
   148  	return it == nil ||
   149  		it.VersionBuild == InvalidVersionValue ||
   150  		it.VersionBuild == constants.Zero
   151  }
   152  
   153  func (it *Version) isInvalidOrEmptyAll() bool {
   154  	return it.IsMajorInvalidOrZero() &&
   155  		it.IsMinorInvalidOrZero() &&
   156  		it.IsPatchInvalidOrZero() &&
   157  		it.IsBuildInvalidOrZero()
   158  }
   159  
   160  func (it *Version) IsEmptyOrInvalid() bool {
   161  	return it == nil ||
   162  		it.VersionDisplay() == "" ||
   163  		it.isInvalidOrEmptyAll()
   164  }
   165  
   166  func (it *Version) HasAnyItem() bool {
   167  	return !it.IsEmptyOrInvalid()
   168  }
   169  
   170  func (it *Version) IsDefined() bool {
   171  	return !it.IsEmptyOrInvalid()
   172  }
   173  
   174  func (it *Version) IsInvalid() bool {
   175  	return it.IsEmptyOrInvalid()
   176  }
   177  
   178  func (it *Version) IsInvalidOrEmpty() bool {
   179  	return it.IsEmptyOrInvalid()
   180  }
   181  
   182  func (it *Version) IsVersionCompareNotEqual(
   183  	versionCompact string,
   184  ) bool {
   185  	return !it.IsVersionCompareEqual(
   186  		versionCompact)
   187  }
   188  
   189  func (it *Version) IsVersionCompareEqual(
   190  	versionCompact string,
   191  ) bool {
   192  	if it == nil && versionCompact == "" {
   193  		return true
   194  	}
   195  
   196  	if it == nil && versionCompact != "" {
   197  		return false
   198  	}
   199  
   200  	return it.VersionCompact == versionCompact
   201  }
   202  
   203  func (it *Version) ValueByIndex(
   204  	index versionindexes.Index,
   205  ) int {
   206  	switch index {
   207  	case versionindexes.Major:
   208  		return it.VersionMajor
   209  	case versionindexes.Minor:
   210  		return it.VersionMinor
   211  	case versionindexes.Patch:
   212  		return it.VersionPatch
   213  	case versionindexes.Build:
   214  		return it.VersionBuild
   215  	}
   216  
   217  	return InvalidVersionValue
   218  }
   219  
   220  func (it *Version) ValueByIndexes(
   221  	indexes ...versionindexes.Index,
   222  ) []int {
   223  	slice := make([]int, len(indexes))
   224  
   225  	for i, index := range indexes {
   226  		slice[i] = it.ValueByIndex(index)
   227  	}
   228  
   229  	return slice
   230  }
   231  
   232  func (it *Version) AllVersionValues() []int {
   233  	return it.ValueByIndexes(versionindexes.AllVersionIndexes...)
   234  }
   235  
   236  func (it *Version) AllValidVersionValues() []int {
   237  	slice := it.AllValidVersionValues()
   238  
   239  	for i, item := range slice {
   240  		if item == InvalidVersionValue {
   241  			slice = append(slice[:i], slice[i+1:]...)
   242  		}
   243  	}
   244  
   245  	return slice
   246  }
   247  
   248  func (it *Version) Major(comparingMajor int) corecomparator.Compare {
   249  	return corecmp.Integer(it.VersionMajor, comparingMajor)
   250  }
   251  
   252  func (it *Version) IsMajorAtLeast(comparingMajor int) bool {
   253  	return corecmp.Integer(it.VersionMajor, comparingMajor).
   254  		IsLeftGreaterOrGreaterEqualOrEqual()
   255  }
   256  
   257  func (it *Version) IsMajorStringAtLeast(comparingMajor string) bool {
   258  	// fine to swallow error
   259  	majorInt, _ := strconv.Atoi(comparingMajor)
   260  
   261  	return corecmp.Integer(it.VersionMajor, majorInt).
   262  		IsLeftGreaterOrGreaterEqualOrEqual()
   263  }
   264  
   265  func (it *Version) IsMajorMinorAtLeast(
   266  	major, minor int,
   267  ) bool {
   268  	return it.MajorMinor(major, minor).
   269  		IsLeftGreaterOrGreaterEqualOrEqual()
   270  }
   271  func (it *Version) IsMajorBuildAtLeast(
   272  	major, build int,
   273  ) bool {
   274  	return it.MajorBuild(major, build).
   275  		IsLeftGreaterEqualLogically()
   276  }
   277  
   278  func (it *Version) IsMajorMinorPatchAtLeast(
   279  	major,
   280  	minor,
   281  	patch int,
   282  ) bool {
   283  	cmp := it.MajorMinorPatch(
   284  		major,
   285  		minor,
   286  		patch,
   287  	)
   288  
   289  	return cmp.
   290  		IsLeftGreaterOrGreaterEqualOrEqual()
   291  }
   292  
   293  func (it *Version) MajorMinor(
   294  	major,
   295  	minor int,
   296  ) corecomparator.Compare {
   297  	majorCmp := corecmp.Integer(
   298  		it.VersionMajor, major)
   299  
   300  	if majorCmp.IsNotEqualLogically() {
   301  		return majorCmp
   302  	}
   303  
   304  	minorCmp := corecmp.Integer(
   305  		it.VersionMinor, minor)
   306  
   307  	if minorCmp.IsNotEqualLogically() {
   308  		return minorCmp
   309  	}
   310  
   311  	return corecomparator.Equal
   312  }
   313  
   314  func (it *Version) MajorMinorPatchBuildString(
   315  	major,
   316  	minor,
   317  	build,
   318  	patch string,
   319  ) corecomparator.Compare {
   320  	// fine to swallow error
   321  	majorInt, _ := strconv.Atoi(major)
   322  	minorInt, _ := strconv.Atoi(minor)
   323  	patchInt, _ := strconv.Atoi(patch)
   324  	buildInt, _ := strconv.Atoi(build)
   325  
   326  	return it.MajorMinorPatchBuild(
   327  		majorInt,
   328  		minorInt,
   329  		patchInt,
   330  		buildInt)
   331  }
   332  
   333  func (it *Version) MajorBuildString(
   334  	major,
   335  	build string,
   336  ) corecomparator.Compare {
   337  	// fine to swallow error
   338  	majorInt, _ := strconv.Atoi(major)
   339  	buildInt, _ := strconv.Atoi(build)
   340  
   341  	return it.MajorBuild(
   342  		majorInt, buildInt)
   343  }
   344  
   345  func (it *Version) MajorBuild(
   346  	major,
   347  	build int,
   348  ) corecomparator.Compare {
   349  	majorCmp := corecmp.Integer(
   350  		it.VersionMajor, major)
   351  
   352  	if majorCmp.IsNotEqualLogically() {
   353  		return majorCmp
   354  	}
   355  
   356  	buildCmp := corecmp.Integer(
   357  		it.VersionBuild, build)
   358  
   359  	if buildCmp.IsNotEqualLogically() {
   360  		return buildCmp
   361  	}
   362  
   363  	return corecomparator.Equal
   364  }
   365  
   366  func (it *Version) Patch(
   367  	patch int,
   368  ) corecomparator.Compare {
   369  	patchCmp := corecmp.Integer(
   370  		it.VersionPatch, patch)
   371  
   372  	if patchCmp.IsNotEqualLogically() {
   373  		return patchCmp
   374  	}
   375  
   376  	return corecomparator.Equal
   377  }
   378  
   379  func (it *Version) MajorPatch(
   380  	major,
   381  	patch int,
   382  ) corecomparator.Compare {
   383  	majorCmp := corecmp.Integer(
   384  		it.VersionMajor, major)
   385  
   386  	if majorCmp.IsNotEqualLogically() {
   387  		return majorCmp
   388  	}
   389  
   390  	patchCmp := corecmp.Integer(
   391  		it.VersionPatch, patch)
   392  
   393  	if patchCmp.IsNotEqualLogically() {
   394  		return patchCmp
   395  	}
   396  
   397  	return corecomparator.Equal
   398  }
   399  
   400  func (it *Version) Build(
   401  	build int,
   402  ) corecomparator.Compare {
   403  	buildCmp := corecmp.Integer(
   404  		it.VersionBuild, build)
   405  
   406  	if buildCmp.IsNotEqualLogically() {
   407  		return buildCmp
   408  	}
   409  
   410  	return corecomparator.Equal
   411  }
   412  
   413  func (it *Version) MajorMinorPatch(
   414  	major,
   415  	minor,
   416  	patch int,
   417  ) corecomparator.Compare {
   418  	majorMinor := it.MajorMinor(major, minor)
   419  
   420  	if majorMinor.IsNotEqualLogically() {
   421  		return majorMinor
   422  	}
   423  
   424  	patchCmp := corecmp.Integer(
   425  		it.VersionPatch,
   426  		patch)
   427  
   428  	if patchCmp.IsNotEqualLogically() {
   429  		return patchCmp
   430  	}
   431  
   432  	return corecomparator.Equal
   433  }
   434  
   435  func (it *Version) MajorMinorPatchBuild(
   436  	major,
   437  	minor,
   438  	patch,
   439  	build int,
   440  ) corecomparator.Compare {
   441  	majorMinorPatch := it.MajorMinorPatch(
   442  		major,
   443  		minor,
   444  		patch)
   445  
   446  	if majorMinorPatch.IsNotEqualLogically() {
   447  		return majorMinorPatch
   448  	}
   449  
   450  	// everything equal before
   451  	buildCmp := it.Build(build)
   452  
   453  	if buildCmp.IsNotEqualLogically() {
   454  		return buildCmp
   455  	}
   456  
   457  	return corecomparator.Equal
   458  }
   459  
   460  func (it *Version) Compare(
   461  	right *Version,
   462  ) corecomparator.Compare {
   463  	return Compare(it, right)
   464  }
   465  
   466  func (it *Version) IsEqual(
   467  	right *Version,
   468  ) bool {
   469  	return Compare(it, right).IsEqual()
   470  }
   471  
   472  // IsLeftLessThan it < right
   473  func (it *Version) IsLeftLessThan(
   474  	right *Version,
   475  ) bool {
   476  	return Compare(it, right).IsLeftLess()
   477  }
   478  
   479  // IsLeftGreaterThan it > right
   480  func (it *Version) IsLeftGreaterThan(
   481  	right *Version,
   482  ) bool {
   483  	return Compare(it, right).IsLeftGreater()
   484  }
   485  
   486  // IsLeftLessThanOrEqual it <= right
   487  func (it *Version) IsLeftLessThanOrEqual(
   488  	right *Version,
   489  ) bool {
   490  	return Compare(it, right).IsLeftLessOrLessEqualOrEqual()
   491  }
   492  
   493  // IsLeftGreaterThanOrEqual it >= right
   494  func (it *Version) IsLeftGreaterThanOrEqual(
   495  	right *Version,
   496  ) bool {
   497  	return Compare(it, right).IsLeftGreaterOrGreaterEqualOrEqual()
   498  }
   499  
   500  func (it *Version) IsExpectedComparison(
   501  	expectedComparison corecomparator.Compare,
   502  	right *Version,
   503  ) bool {
   504  	return expectedComparison.IsCompareEqualLogically(
   505  		Compare(it, right))
   506  }
   507  
   508  // IsExpectedComparisonUsingVersionString
   509  //
   510  //  @Description: it returns the expected comparison result
   511  //  @param expectedComparison
   512  //  @param rightVersion : can have "v0.0.0" or "0.0.0" or "v0.0.0.0" or "v0" or "v0.1"
   513  //
   514  //  @return bool
   515  func (it *Version) IsExpectedComparisonUsingVersionString(
   516  	expectedComparison corecomparator.Compare,
   517  	rightVersion string,
   518  ) bool {
   519  	return it.IsExpectedComparison(
   520  		expectedComparison,
   521  		New.Default(rightVersion),
   522  	)
   523  }
   524  
   525  // IsAtLeast
   526  //
   527  //  @Description: it returns the true if current version is at same or above as the given one or more
   528  //  @param rightVersion : can have "v0.0.0" or "0.0.0" or "v0.0.0.0" or "v0" or "v0.1"
   529  //
   530  //  @return bool
   531  func (it *Version) IsAtLeast(
   532  	rightVersion string,
   533  ) bool {
   534  	return it.IsExpectedComparison(
   535  		corecomparator.LeftGreaterEqual,
   536  		New.Default(rightVersion),
   537  	)
   538  }
   539  
   540  // IsEqualVersionString
   541  //
   542  //  @Description: it returns the true if current version same by deduction
   543  //  @param rightVersion : can have "v0.0.0" or "0.0.0" or "v0.0.0.0" or "v0" or "v0.1"
   544  //
   545  //  @return bool
   546  func (it *Version) IsEqualVersionString(
   547  	rightVersion string,
   548  ) bool {
   549  	return it.IsExpectedComparison(
   550  		corecomparator.Equal,
   551  		New.Default(rightVersion),
   552  	)
   553  }
   554  
   555  // IsLowerVersionString
   556  //
   557  //  @Description: it returns the true if current version less than the given version.
   558  //  @param rightVersion : can have "v0.0.0" or "0.0.0" or "v0.0.0.0" or "v0" or "v0.1"
   559  //
   560  //  @return bool
   561  func (it *Version) IsLowerVersionString(
   562  	rightVersion string,
   563  ) bool {
   564  	return it.IsExpectedComparison(
   565  		corecomparator.LeftLess,
   566  		New.Default(rightVersion),
   567  	)
   568  }
   569  
   570  // IsLowerEqualVersionString
   571  //
   572  //  @Description: it returns the true if current version less or equal than the given version.
   573  //  @param rightVersion : can have "v0.0.0" or "0.0.0" or "v0.0.0.0" or "v0" or "v0.1"
   574  //
   575  //  @return bool
   576  func (it *Version) IsLowerEqualVersionString(
   577  	rightVersion string,
   578  ) bool {
   579  	return it.IsExpectedComparison(
   580  		corecomparator.LeftLessEqual,
   581  		New.Default(rightVersion),
   582  	)
   583  }
   584  
   585  func (it *Version) ComparisonValueIndexes(
   586  	right *Version,
   587  	indexes ...versionindexes.Index,
   588  ) corecomparator.Compare {
   589  	r, isApplicable := hasDeductUsingNilNess(it, right)
   590  
   591  	if isApplicable {
   592  		return r
   593  	}
   594  
   595  	leftVersions := make([]int, len(indexes))
   596  	rightVersions := make([]int, len(indexes))
   597  	for i, index := range indexes {
   598  		leftVersions[i] = it.ValueByIndex(index)
   599  		rightVersions[i] = right.ValueByIndex(index)
   600  	}
   601  
   602  	return corecmp.VersionSliceInteger(
   603  		leftVersions,
   604  		rightVersions)
   605  }
   606  
   607  func (it Version) Clone() Version {
   608  	return Version{
   609  		VersionCompact: it.VersionCompact,
   610  		VersionMajor:   it.VersionMajor,
   611  		VersionMinor:   it.VersionMinor,
   612  		VersionPatch:   it.VersionPatch,
   613  		VersionBuild:   it.VersionBuild,
   614  	}
   615  }
   616  
   617  func (it *Version) ClonePtr() *Version {
   618  	if it == nil {
   619  		return nil
   620  	}
   621  
   622  	return &Version{
   623  		VersionCompact: it.VersionCompact,
   624  		VersionMajor:   it.VersionMajor,
   625  		VersionMinor:   it.VersionMinor,
   626  		VersionPatch:   it.VersionPatch,
   627  		VersionBuild:   it.VersionBuild,
   628  	}
   629  }
   630  
   631  func (it Version) NonPtr() Version {
   632  	return it
   633  }
   634  
   635  func (it *Version) Ptr() *Version {
   636  	return it
   637  }
   638  
   639  func (it Version) Json() corejson.Result {
   640  	return corejson.New(it)
   641  }
   642  
   643  func (it Version) JsonPtr() *corejson.Result {
   644  	return corejson.NewPtr(it)
   645  }
   646  
   647  func (it *Version) JsonParseSelfInject(jsonResult *corejson.Result) error {
   648  	return jsonResult.Deserialize(it)
   649  }
   650  
   651  func (it *Version) AsJsonContractsBinder() corejson.JsonContractsBinder {
   652  	return it
   653  }