Skip to content

Commit

Permalink
adding some internals comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gravataLonga committed Jul 18, 2022
1 parent 3ccb324 commit 54a77d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions object/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
)

// EPSILON is used only for internally usage only for now.
var EPSILON float64 = 0.00000001

type Float struct {
Expand Down
11 changes: 10 additions & 1 deletion object/typing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"strings"
)

// CheckFunc signature for check arguments function
type CheckFunc func(name string, args []Object) error

// Check arguments passed to object call we can pass many CheckFunc as you want
func Check(name string, args []Object, checks ...CheckFunc) error {
for _, check := range checks {
if err := check(name, args); err != nil {
Expand All @@ -16,6 +18,7 @@ func Check(name string, args []Object, checks ...CheckFunc) error {
return nil
}

// ExactArgs expect exact nums arguments
func ExactArgs(n int) CheckFunc {
return func(name string, args []Object) error {
if len(args) != n {
Expand All @@ -27,6 +30,8 @@ func ExactArgs(n int) CheckFunc {
return nil
}
}

// MinimumArgs check if have at least n arguments
func MinimumArgs(n int) CheckFunc {
return func(name string, args []Object) error {
if len(args) < n {
Expand All @@ -38,6 +43,8 @@ func MinimumArgs(n int) CheckFunc {
return nil
}
}

// RangeOfArgs expect n until m arguments
func RangeOfArgs(n, m int) CheckFunc {
return func(name string, args []Object) error {
if len(args) < n || len(args) > m {
Expand All @@ -49,6 +56,8 @@ func RangeOfArgs(n, m int) CheckFunc {
return nil
}
}

// WithTypes combined with ExactArgs it will check if we got ObjectType by it is order.
func WithTypes(types ...ObjectType) CheckFunc {
return func(name string, args []Object) error {
for i, t := range types {
Expand All @@ -63,6 +72,7 @@ func WithTypes(types ...ObjectType) CheckFunc {
}
}

// OneOfType almost same as WithTypes but check one of type of ObjectType
func OneOfType(types ...ObjectType) CheckFunc {
return func(name string, args []Object) error {
for _, vt := range types {
Expand All @@ -83,7 +93,6 @@ func OneOfType(types ...ObjectType) CheckFunc {
got[i] = string(t.Type())
}

// expected argument to be STRING,ARRAY, got INTEGER
return fmt.Errorf(
"TypeError: %s() expected argument to be `%s` got `%s`",
name, strings.Join(expected, ","), strings.Join(got, ","),
Expand Down

0 comments on commit 54a77d9

Please sign in to comment.