3
0

Define TypedValue comparators; Make parser init more flexible

This commit is contained in:
Tomaž Jerman
2021-05-30 14:50:02 +02:00
parent ddebcf6457
commit 98c16227b2
7 changed files with 241 additions and 6 deletions
+27
View File
@@ -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 }}
+4 -1
View File
@@ -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