Skip to content

Commit

Permalink
feat: change to file location
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 11, 2023
1 parent 18f47b7 commit 6888a96
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 159 deletions.
2 changes: 1 addition & 1 deletion internal/parse/error.go → error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package parse
package jsonata

import "fmt"

Expand Down
99 changes: 0 additions & 99 deletions internal/parse/util.go

This file was deleted.

110 changes: 55 additions & 55 deletions internal/parse/signature.go → signature.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package parse
package jsonata

import (
"reflect"
Expand All @@ -7,12 +7,12 @@ import (
)

type (
Validator struct {
validator struct {

Check failure on line 10 in signature.go

View workflow job for this annotation

GitHub Actions / Check ubuntu-20.04 @ Go 1.21

type validator is unused (U1000)

Check failure on line 10 in signature.go

View workflow job for this annotation

GitHub Actions / Check macos-latest @ Go 1.21

type validator is unused (U1000)
Definition string
Validate func(args []any, ctx any) ([]any, error)
}

Param struct {
param struct {

Check failure on line 15 in signature.go

View workflow job for this annotation

GitHub Actions / Check ubuntu-20.04 @ Go 1.21

type param is unused (U1000)

Check failure on line 15 in signature.go

View workflow job for this annotation

GitHub Actions / Check macos-latest @ Go 1.21

type param is unused (U1000)
Regex *regexp.Regexp
Type string
Subtype string
Expand All @@ -33,18 +33,18 @@ var (
}
)

// ParseSignature Parses a function signature definition and returns a validation function
func ParseSignature(signature string) (*Validator, error) {
// parseSignature Parses a function signature definition and returns a validation function
func parseSignature(signature string) (*validator, error) {

Check failure on line 37 in signature.go

View workflow job for this annotation

GitHub Actions / Check ubuntu-20.04 @ Go 1.21

func parseSignature is unused (U1000)

Check failure on line 37 in signature.go

View workflow job for this annotation

GitHub Actions / Check macos-latest @ Go 1.21

func parseSignature is unused (U1000)
signatureRunes := []rune(signature)

// create a Regex that represents this signature and return a function that when invoked,
// returns the validated (possibly fixed-up) arguments, or throws a validation error
// step through the signature, one symbol at a time
var (
position = 1
params = []Param{}
param = Param{}
prevParam Param = param
params = []param{}
currParam = param{}
prevParam param = currParam
)
for position < len(signatureRunes) {
symbol := signatureRunes[position]
Expand All @@ -55,9 +55,9 @@ func ParseSignature(signature string) (*Validator, error) {
}

next := func() {
params = append(params, param)
prevParam = param
param = Param{}
params = append(params, currParam)
prevParam = currParam
currParam = param{}
}

findClosingBracket := func(str []rune, start int, openSymbol rune, closeSymbol rune) int {
Expand Down Expand Up @@ -86,43 +86,43 @@ func ParseSignature(signature string) (*Validator, error) {
if regex, err := regexp.Compile("[" + string(symbol) + "m]"); err != nil {
return nil, err
} else {
param.Regex = regex
currParam.Regex = regex
}
param.Type = string(symbol)
currParam.Type = string(symbol)
next()
case 'a': // array
// normally treat any value as singleton array
if regex, err := regexp.Compile("[asnblfom]"); err != nil {
return nil, err
} else {
param.Regex = regex
currParam.Regex = regex
}
param.Type = string(symbol)
param.Array = true
currParam.Type = string(symbol)
currParam.Array = true
next()
case 'f': // function
if regex, err := regexp.Compile("f"); err != nil {
return nil, err
} else {
param.Regex = regex
currParam.Regex = regex
}
param.Type = string(symbol)
currParam.Type = string(symbol)
next()
case 'j': // any JSON type
if regex, err := regexp.Compile("[asnblom]"); err != nil {
return nil, err
} else {
param.Regex = regex
currParam.Regex = regex
}
param.Type = string(symbol)
currParam.Type = string(symbol)
next()
case 'x': // any type
if regex, err := regexp.Compile("[asnblfom]"); err != nil {
return nil, err
} else {
param.Regex = regex
currParam.Regex = regex
}
param.Type = string(symbol)
currParam.Type = string(symbol)
next()
case '-': // use context if param not supplied
prevParam.Context = true
Expand All @@ -147,7 +147,7 @@ func ParseSignature(signature string) (*Validator, error) {
if regex, err := regexp.Compile("[" + string(choice) + "m]"); err != nil {
return nil, err
} else {
param.Regex = regex
currParam.Regex = regex
}
} else {
// TODO harder
Expand All @@ -157,7 +157,7 @@ func ParseSignature(signature string) (*Validator, error) {
Offset: position,
}
}
param.Type = "(" + string(choice) + ")"
currParam.Type = "(" + string(choice) + ")"
position = endParen
next()
case '<': // type parameter - can only be applied to 'a' and 'f'
Expand Down Expand Up @@ -188,35 +188,6 @@ func ParseSignature(signature string) (*Validator, error) {
return nil, err
}

getSymbol := func(value any) rune {
if IsNil(value) {
return 'l'
}

t := reflect.TypeOf(value)
for t.Kind() == reflect.Pointer {
t = t.Elem()
}

switch t.Kind() {
case reflect.String:
return 's'
case reflect.Int, reflect.Uint, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
return 'n'
case reflect.Bool:
return 'b'
case reflect.Func:
return 'f'
case reflect.Array, reflect.Slice:
return 'a'
case reflect.Map, reflect.Struct:
return 'o'
default:
// any value can be undefined, but should be allowed to match
return 'm' // m for missing
}
}

throwValidationError := func(badArgs []any, badSig string) error {
// to figure out where this went wrong we need apply each component of the
// regex to each argument until we get to the one that fails to match
Expand Down Expand Up @@ -248,7 +219,7 @@ func ParseSignature(signature string) (*Validator, error) {
}
}

return &Validator{
return &validator{
Definition: signature,
Validate: func(args []any, context any) ([]any, error) {
suppliedSig := &strings.Builder{}
Expand Down Expand Up @@ -301,7 +272,7 @@ func ParseSignature(signature string) (*Validator, error) {
var itemType rune
var differentItems []any

ForEach(arg, func(k, v any) bool {
forEach(arg, func(k, v any) bool {
if itemType == 0 {
itemType = getSymbol(v)
if itemType != []rune(param.Subtype)[0] { // TODO recurse further
Expand Down Expand Up @@ -350,3 +321,32 @@ func ParseSignature(signature string) (*Validator, error) {
},
}, nil
}

func getSymbol(value any) rune {

Check failure on line 325 in signature.go

View workflow job for this annotation

GitHub Actions / Check ubuntu-20.04 @ Go 1.21

func getSymbol is unused (U1000)

Check failure on line 325 in signature.go

View workflow job for this annotation

GitHub Actions / Check macos-latest @ Go 1.21

func getSymbol is unused (U1000)
if isNil(value) {
return 'l'
}

t := reflect.TypeOf(value)
for t.Kind() == reflect.Pointer {
t = t.Elem()
}

switch t.Kind() {
case reflect.String:
return 's'
case reflect.Int, reflect.Uint, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
return 'n'
case reflect.Bool:
return 'b'
case reflect.Func:
return 'f'
case reflect.Array, reflect.Slice:
return 'a'
case reflect.Map, reflect.Struct:
return 'o'
default:
// any value can be undefined, but should be allowed to match
return 'm' // m for missing
}
}
Loading

0 comments on commit 6888a96

Please sign in to comment.