Define TypedValue comparators; Make parser init more flexible
This commit is contained in:
@@ -75,6 +75,33 @@ func (t *{{ $exprType }}) Assign(val interface{}) (error) {
|
||||
}
|
||||
}
|
||||
|
||||
{{ if $def.Comparable }}
|
||||
{{ if not $def.CustomComparator }}
|
||||
// Compare the two {{ $exprType }} values
|
||||
func (t {{ $exprType }}) Compare(to TypedValue) (int, error) {
|
||||
c, err := New{{ $exprType }}(to)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("cannot compare %s and %s: %s", t.Type(), c.Type(), err.Error())
|
||||
}
|
||||
|
||||
switch {
|
||||
case t.value == c.value:
|
||||
return 0, nil
|
||||
case t.value < c.value:
|
||||
return -1, nil
|
||||
case t.value > c.value:
|
||||
return 1, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("cannot compare %s and %s: unknown state", t.Type(), c.Type())
|
||||
}
|
||||
}
|
||||
{{ else }}
|
||||
// Compare the two {{ $exprType }} values
|
||||
func (t {{ $exprType }}) Compare(to TypedValue) (int, error) {
|
||||
return compareTo{{ $exprType }}(t, to)
|
||||
}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ if $def.Struct }}
|
||||
{{ if not $def.CustomFieldAssigner }}
|
||||
|
||||
@@ -2,10 +2,11 @@ package codegen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"path"
|
||||
"text/template"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -32,6 +33,8 @@ type (
|
||||
CustomGValSelector bool `yaml:"customGValSelector"`
|
||||
CustomSelector bool `yaml:"customSelector"`
|
||||
CustomFieldAssigner bool `yaml:"customFieldAssigner"`
|
||||
Comparable bool `yaml:"comparable"`
|
||||
CustomComparator bool `yaml:"customComparator"`
|
||||
Struct []*exprTypeStructDef
|
||||
|
||||
// @todo custom setters
|
||||
|
||||
Reference in New Issue
Block a user