From: Sergey Matveev Date: Sat, 5 Apr 2025 07:34:31 +0000 (+0300) Subject: Widen comparable types for EQ command X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=dd0de92783097cb9a852260c058f065969e34c8fb7451d4b68f0a97e28fb5ccd;p=keks.git Widen comparable types for EQ command --- diff --git a/c/lib/schema.c b/c/lib/schema.c index 7251dd3..988d359 100644 --- a/c/lib/schema.c +++ b/c/lib/schema.c @@ -257,9 +257,9 @@ Eached: return err; } err.offSchema = schema->offsets[idxSchema]; - if (schema->list[idxSchema].atom.typ != KEKSItemStr) { + if (schema->list[idxSchema].atom.typ != KEKSItemBin) { err.code = KEKSSchemaErrInvalidSchema; - err.msg = "non-str EQ"; + err.msg = "non-bin EQ"; return err; } err.code = ((schema->list[idxSchema].atom.v.str.len == diff --git a/go/schema/check.go b/go/schema/check.go index 9e0f2e6..ffc8aa8 100644 --- a/go/schema/check.go +++ b/go/schema/check.go @@ -16,6 +16,7 @@ package schema import ( + "bytes" "errors" "fmt" "slices" @@ -115,16 +116,35 @@ func Check(schemaName string, schemas map[string][][]any, data any) error { if len(act) != 2 { return fmt.Errorf("%s: %d: %s: wrong number of args", schemaName, i, cmd) } - expect, ok := act[1].(string) + expect, ok := act[1].([]byte) if !ok { - return fmt.Errorf("%s: %d: %s: bad str: %+v", schemaName, i, cmd, act[1]) + return fmt.Errorf("%s: %d: %s: bad bin: %+v", schemaName, i, cmd, act[1]) } for _, v := range vs { - got, ok := v.(string) - if !ok { - return fmt.Errorf("%s: %d: %s: not str: %T", schemaName, i, cmd, v) + var eq bool + switch got := v.(type) { + case string: + eq = got == string(expect) + case []byte: + eq = bytes.Equal(got, expect) + case keks.Hexlet: + eq = bytes.Equal(got[:], expect) + case *tai64n.TAI64: + eq = bytes.Equal(got[:], expect) + case *tai64n.TAI64N: + eq = bytes.Equal(got[:], expect) + case *tai64n.TAI64NA: + eq = bytes.Equal(got[:], expect) + case keks.Magic: + eq = string(got) == string(expect) + case keks.Raw: + eq = bytes.Equal(got, expect) + default: + if !ok { + return fmt.Errorf("%s: %d: %s: non-comparable: %T", schemaName, i, cmd, v) + } } - if got != expect { + if !eq { return fmt.Errorf("%s: %d: %s: !=", schemaName, i, cmd) } } diff --git a/spec/schema/cmds.texi b/spec/schema/cmds.texi index f37f324..6804d67 100644 --- a/spec/schema/cmds.texi +++ b/spec/schema/cmds.texi @@ -60,7 +60,7 @@ maximal time precision. "p" is integer with following possible values: @end itemize @item = v -Check that chosen (if it exists) string element's value equals to "v". +Check that chosen (if it exists) element's value equals to binary string "v". @end table diff --git a/tcl/schema2bin b/tcl/schema2bin index 159054d..23f4d1a 100755 --- a/tcl/schema2bin +++ b/tcl/schema2bin @@ -28,7 +28,7 @@ proc TAKE {v} { proc EXISTS {} {return {{LIST {{STR E}}}}} proc !EXISTS {} {return {{LIST {{STR !E}}}}} proc EACH {} {return {{LIST {{STR *}}}}} -proc EQ {v} {subst {{LIST {{STR =} {STR $v}}}}} +proc EQ {v} {subst {{LIST {{STR =} {BIN $v}}}}} proc TYPE {vs} { set l {{STR T}} foreach v $vs {