Skip to content
Snippets Groups Projects
Select Git revision
  • feature/v1.3.60
  • develop default protected
  • main protected
  • release/v1.0.7
  • release/v1.0.0
  • feature/v0.9.5
  • release/v0.6.2
  • hotfix/v0.5.7
  • release/v0.5.6
  • release/v0.5.5
  • hotfix/v0.5.4
  • hotfix/v0.5.3
  • hotfix/v0.5.2
  • release/v0.5.1
  • hotfix/v0.5.0
  • hotfix/v0.4.9
  • hotfix/v0.4.8
  • hotfix/v0.4.7
  • release/v0.4.6
  • hotfix/v0.4.5
  • v1.3.55
  • v1.3.52
  • v1.3.50
  • v1.3.45
  • v1.3.42
  • v1.3.41
  • v1.3.40
  • v1.3.38
  • v1.3.37
  • v1.3.36
  • v1.3.35
  • v1.3.34
  • v1.3.33
  • v1.3.32
  • v1.3.31
  • v1.3.30
  • v1.3.29
  • v1.3.28
  • v1.3.27
  • v1.3.26
40 results

VersionsCollection.go

VersionsCollection.go 3.33 KiB
package coreversion

import (
	"strings"

	"gitlab.com/evatix-go/core/constants"
	"gitlab.com/evatix-go/core/coredata/corejson"
	"gitlab.com/evatix-go/core/coreinterface"
)

type VersionsCollection struct {
	Versions []*Version
}

func (it *VersionsCollection) Add(
	version string,
) *VersionsCollection {
	it.Versions = append(
		it.Versions,
		New.Create(version))

	return it
}

func (it *VersionsCollection) AddSkipInvalid(
	version string,
) *VersionsCollection {
	v := New.Create(version)

	if v.IsEmptyOrInvalid() {
		return it
	}

	it.Versions = append(
		it.Versions,
		v)

	return it
}

func (it *VersionsCollection) AddVersionsRaw(
	versions ...string,
) *VersionsCollection {
	for _, v := range versions {
		it.Versions = append(
			it.Versions,
			New.Create(v))
	}

	return it
}

func (it *VersionsCollection) AddVersions(
	versions ...*Version,
) *VersionsCollection {
	for _, v := range versions {
		it.Versions = append(
			it.Versions, v)
	}

	return it
}

func (it *VersionsCollection) Length() int {
	if it == nil {
		return 0
	}

	return len(it.Versions)
}

func (it *VersionsCollection) Count() int {
	return it.Length()
}

func (it *VersionsCollection) IsEmpty() bool {
	return it.Length() == 0
}

func (it *VersionsCollection) HasAnyItem() bool {
	return it.Length() > 0
}

func (it *VersionsCollection) LastIndex() int {
	return it.Length() - 1
}

func (it *VersionsCollection) HasIndex(index int) bool {
	return it.LastIndex() >= index
}

func (it *VersionsCollection) VersionCompactStrings() []string {
	if it.IsEmpty() {
		return []string{}
	}

	slice := make([]string, it.Length())

	for i, version := range it.Versions {
		slice[i] = version.VersionCompact
	}

	return slice
}

func (it *VersionsCollection) VersionsStrings() []string {
	if it.IsEmpty() {
		return []string{}
	}

	slice := make([]string, it.Length())

	for i, version := range it.Versions {
		slice[i] = version.VersionDisplay()
	}

	return slice
}

func (it *VersionsCollection) IndexOf(
	versionString string,
) int {
	lookupVersion := New.Create(versionString)

	for i, version := range it.Versions {
		if version.VersionCompact == lookupVersion.VersionCompact {
			return i
		}
	}

	return constants.InvalidValue
}

func (it *VersionsCollection) IsContainsVersion(
	versionString string,
) bool {
	return it.IndexOf(versionString) > constants.InvalidValue
}

func (it *VersionsCollection) IsEqual(
	another *VersionsCollection,
) bool {
	if it == nil && another == nil {
		return true
	}

	if it == nil || another == nil {
		return false
	}

	if it.Length() != another.Length() {
		return false
	}

	for i, version := range it.Versions {
		anotherV := another.Versions[i]

		if version.IsVersionCompareNotEqual(anotherV.VersionCompact) {
			return false
		}
	}

	return true
}

func (it *VersionsCollection) String() string {
	return strings.Join(it.VersionsStrings(), constants.NewLineUnix)
}

func (it *VersionsCollection) Json() corejson.Result {
	return corejson.New(it)
}

func (it *VersionsCollection) JsonPtr() *corejson.Result {
	return corejson.NewPtr(it)
}

func (it *VersionsCollection) JsonParseSelfInject(jsonResult *corejson.Result) error {
	return jsonResult.Deserialize(it)
}

func (it *VersionsCollection) AsJsonContractsBinder() corejson.JsonContractsBinder {
	return it
}

func (it *VersionsCollection) AsBasicSliceContractsBinder() coreinterface.BasicSlicerContractsBinder {
	return it
}