Files
expr/types/interval/interval.go
T

81 lines
1.5 KiB
Go

// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// interval.go
package interval
import (
"fmt"
"git.portale-stac.it/go-pkg/expr/kern"
"git.portale-stac.it/go-pkg/expr/types"
)
const TypeName = "interval"
type IntervalType struct {
begin, end, step int64
}
func NewInterval(begin, end, step int64) *IntervalType {
return &IntervalType{
begin: begin,
end: end,
step: step,
}
}
func (p *IntervalType) Begin() int64 {
return p.begin
}
func (p *IntervalType) End() int64 {
return p.end
}
func (p *IntervalType) Step() int64 {
return p.step
}
func IsInterval(v any) (ok bool) {
_, ok = v.(*IntervalType)
return ok
}
func (p *IntervalType) TypeName() string {
return TypeName
}
func (p *IntervalType) ToString(opt kern.FmtOpt) string {
return fmt.Sprintf("%d..%d..%d", p.begin, p.end, p.step)
}
func (p *IntervalType) String() string {
return p.ToString(0)
}
func (p *IntervalType) ToIntTriple() (b, e, s int) {
b = int(p.begin)
e = int(p.end)
s = int(p.step)
return
}
func (p *IntervalType) Contains(value any) (ok bool) {
if v, err := types.ToGoInt64(value, TypeName); err == nil {
if p.begin <= p.end {
ok = (v >= p.begin && v < p.end && (v-p.begin)%p.step == 0)
} else {
ok = (v <= p.begin && v > p.end && (v-p.begin)%p.step == 0)
}
}
return
}
// func (b *bool) EqualTo(other kern.Equaler) (equal bool) {
// if otherBool, ok := other.(*bool); ok {
// equal = b == otherBool
// }
// return
// }