eval/vals: Comment and rename.

This commit is contained in:
Qi Xiao 2018-03-15 21:26:45 +00:00
parent 873d8eeeaf
commit 2e51774b81
2 changed files with 23 additions and 13 deletions

View File

@ -138,7 +138,7 @@ func (gp GlobPattern) Concat(v interface{}) (interface{}, error) {
return gp, nil
}
return nil, vals.ErrCatNotImplemented
return nil, vals.ErrConcatNotImplemented
}
func (gp GlobPattern) RConcat(v interface{}) (interface{}, error) {
@ -150,7 +150,7 @@ func (gp GlobPattern) RConcat(v interface{}) (interface{}, error) {
return GlobPattern{glob.Pattern{Segments: segs}, gp.Flags, gp.Buts}, nil
}
return nil, vals.ErrCatNotImplemented
return nil, vals.ErrConcatNotImplemented
}
func (gp *GlobPattern) mustGetLastWildSeg() glob.Wild {

View File

@ -5,18 +5,28 @@ import (
"fmt"
)
type (
Concatter interface {
Concat(v interface{}) (interface{}, error)
}
// Concatter wraps the Concat method. See Concat for how it is used.
type Concatter interface {
// Concat concatenates the receiver with another value, the receiver being
// the left operand. If concatenation is not supported for the given value,
// the method can return the special error type ErrCatNotImplemented.
Concat(v interface{}) (interface{}, error)
}
RConcatter interface {
RConcat(v interface{}) (interface{}, error)
}
)
// RConcatter wraps the RConcat method. See Concat for how it is used.
type RConcatter interface {
RConcat(v interface{}) (interface{}, error)
}
var ErrCatNotImplemented = errors.New("cat not implemented")
// ErrConcatNotImplemented is a special error value used to signal that
// concatenation is not implemented. See Concat for how it is used.
var ErrConcatNotImplemented = errors.New("cat not implemented")
// Concat concatenates two values. If both operands are strings, it returns lhs
// + rhs, nil. If the left operand implements Concatter, it calls
// lhs.Concat(rhs). If lhs doesn't implement the interface or returned
// ErrConcatNotImplemented, it then calls rhs.RConcat(lhs). If all attempts
// fail, it returns nil and an error.
func Concat(lhs, rhs interface{}) (interface{}, error) {
if v, ok := tryConcatBuiltins(lhs, rhs); ok {
return v, nil
@ -24,14 +34,14 @@ func Concat(lhs, rhs interface{}) (interface{}, error) {
if lhs, ok := lhs.(Concatter); ok {
v, err := lhs.Concat(rhs)
if err != ErrCatNotImplemented {
if err != ErrConcatNotImplemented {
return v, err
}
}
if rhs, ok := rhs.(RConcatter); ok {
v, err := rhs.RConcat(lhs)
if err != ErrCatNotImplemented {
if err != ErrConcatNotImplemented {
return v, err
}
}