Skip to content

Commit

Permalink
refactor(version): export Constraint (#6)
Browse files Browse the repository at this point in the history
* refactor(version): export Constraint + add Version and Operator funcs

* fix: fill operator
  • Loading branch information
DmitriyLewen authored Jun 3, 2024
1 parent 9f59392 commit cf8a8d2
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions pkg/version/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ func init() {

// Constraints is one or more constraint that a version can be checked against.
type Constraints struct {
constraints [][]constraint
constraints [][]Constraint
}

type constraint struct {
version Version
operator operatorFunc
original string
type Constraint struct {
version Version
operator string
operatorFunc operatorFunc
original string
}

// NewConstraints parses a given constraint and returns a new instance of Constraints
func NewConstraints(v string) (Constraints, error) {
var css [][]constraint
var css [][]Constraint
for _, vv := range strings.Split(v, "||") {
// Validate the segment
if !validConstraintRegexp.MatchString(vv) {
Expand All @@ -72,7 +73,7 @@ func NewConstraints(v string) (Constraints, error) {
ss = append(ss, strings.TrimSpace(vv))
}

var cs []constraint
var cs []Constraint
for _, single := range ss {
c, err := newConstraint(single)
if err != nil {
Expand All @@ -89,32 +90,45 @@ func NewConstraints(v string) (Constraints, error) {

}

func newConstraint(c string) (constraint, error) {
func newConstraint(c string) (Constraint, error) {
m := constraintRegexp.FindStringSubmatch(c)
if m == nil {
return constraint{}, xerrors.Errorf("improper constraint: %s", c)
return Constraint{}, xerrors.Errorf("improper constraint: %s", c)
}

v, err := Parse(m[2])
if err != nil {
return constraint{}, xerrors.Errorf("version parse error (%s): %w", m[2], err)
return Constraint{}, xerrors.Errorf("version parse error (%s): %w", m[2], err)
}

return constraint{
version: v,
operator: constraintOperators[m[1]],
original: c,
return Constraint{
version: v,
operator: m[1],
operatorFunc: constraintOperators[m[1]],
original: c,
}, nil
}

func (c constraint) check(v Version) bool {
return c.operator(v, c.version)
func (c Constraint) check(v Version) bool {
return c.operatorFunc(v, c.version)
}

func (c constraint) String() string {
func (c Constraint) String() string {
return c.original
}

func (c Constraint) Version() string {
return c.version.String()
}

func (c Constraint) Operator() string {
return c.operator
}

func (cs Constraints) List() [][]Constraint {
return cs.constraints
}

// Check tests if a version satisfies all the constraints.
func (cs Constraints) Check(v Version) bool {
for _, c := range cs.constraints {
Expand All @@ -140,7 +154,7 @@ func (cs Constraints) String() string {
return strings.Join(csStr, "||")
}

func andCheck(v Version, constraints []constraint) bool {
func andCheck(v Version, constraints []Constraint) bool {
for _, c := range constraints {
if !c.check(v) {
return false
Expand Down

0 comments on commit cf8a8d2

Please sign in to comment.