This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch eo-api-finalize
in repository ego.
View the commit online.
commit f407339d9e3770ffa9daa234d811aaa7920b49b5
Author: Cedric Bail <[email protected]>
AuthorDate: Thu Nov 26 12:01:19 2026 -0600
efl: NewValue and the container constructors take named scalar types
ScalarType is written with ~int, ~string and the rest, so an enum or
any other named scalar satisfies it and NewValue(Mode(2)) compiles.
The constructors then chose the Eina type with a switch on the
dynamic type, which only matches the predeclared ones: the call
failed at run time with "unsupported scalar type", and NewValueSlice
and NewValueMap returned nil. A named value is now stored as its
underlying type, which is what ValueGet already read it back as.
ValueMarshal still keeps a slice or a map of a named type as nested
values, as before: nothing in the Go API can tell the two apart, so
that change would have come without a test.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
---
efl/eort/future.go | 1 -
efl/promise.go | 2 +-
efl/value.go | 94 ++++++++++++++++++-------------
efl/value_container.go | 8 +--
efl/value_named_test.go | 147 ++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 206 insertions(+), 46 deletions(-)
diff --git a/efl/eort/future.go b/efl/eort/future.go
index 4c67493f..f81dc006 100644
--- a/efl/eort/future.go
+++ b/efl/eort/future.go
@@ -180,7 +180,6 @@ func ScalarFromValue[T efl.ScalarType](v *efl.Value) (T, error) {
// ScalarToValue resolves a future of a string, bool or number.
func ScalarToValue[T efl.ScalarType](v T) (*efl.Value, error) {
- // efl.NewValue takes no named type, such as an enum; ValueMarshal does.
return efl.ValueMarshal(v)
}
diff --git a/efl/promise.go b/efl/promise.go
index f7f0af9e..5c00c47f 100644
--- a/efl/promise.go
+++ b/efl/promise.go
@@ -101,7 +101,7 @@ func anyToValue(v *Value) (*Value, error) {
}
// scalarToValue is the value a future of a string, bool or number resolves
-// with. NewValue takes no named type, such as an enum; ValueMarshal does.
+// with.
func scalarToValue[T ScalarType](v T) (*Value, error) { return ValueMarshal(v) }
// deliverPromise passes the result of s, which has just been settled by the
diff --git a/efl/value.go b/efl/value.go
index f768b740..2a7ce1db 100644
--- a/efl/value.go
+++ b/efl/value.go
@@ -162,13 +162,14 @@ type Value struct {
func newValue[T ScalarType](val T) (*Value, error) {
v := &Value{owned: true}
- switch any(val).(type) {
+ norm := predeclared(val)
+ switch norm.(type) {
case int, int32:
v.ptr = C._ego_value_new(C.EINA_VALUE_TYPE_INT)
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(INT) returned nil")
}
- cv := C.int(scalarToInt(val))
+ cv := C.int(scalarToInt(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(INT) failed")
@@ -179,7 +180,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(UINT) returned nil")
}
- cv := C.uint(scalarToUint(val))
+ cv := C.uint(scalarToUint(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(UINT) failed")
@@ -190,7 +191,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(INT64) returned nil")
}
- cv := C.int64_t(scalarToInt64(val))
+ cv := C.int64_t(scalarToInt64(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(INT64) failed")
@@ -201,7 +202,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(UINT64) returned nil")
}
- cv := C.uint64_t(scalarToUint64(val))
+ cv := C.uint64_t(scalarToUint64(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(UINT64) failed")
@@ -212,7 +213,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(SHORT) returned nil")
}
- cv := C.short(scalarToInt16(val))
+ cv := C.short(scalarToInt16(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(SHORT) failed")
@@ -223,7 +224,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(USHORT) returned nil")
}
- cv := C.ushort(scalarToUint16(val))
+ cv := C.ushort(scalarToUint16(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(USHORT) failed")
@@ -234,7 +235,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(FLOAT) returned nil")
}
- cv := C.float(scalarToFloat32(val))
+ cv := C.float(scalarToFloat32(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(FLOAT) failed")
@@ -245,7 +246,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(DOUBLE) returned nil")
}
- cv := C.double(scalarToFloat64(val))
+ cv := C.double(scalarToFloat64(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(DOUBLE) failed")
@@ -256,7 +257,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(STRING) returned nil")
}
- cs := C.CString(scalarToString(val))
+ cs := C.CString(scalarToString(norm))
defer C.free(unsafe.Pointer(cs))
if C._ego_value_set_string(v.ptr, cs) == 0 {
C._ego_value_free(v.ptr)
@@ -270,7 +271,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
return nil, errors.New("efl: eina_value_new(BOOL) returned nil")
}
var cv C.uchar
- if scalarToBool(val) {
+ if scalarToBool(norm) {
cv = 1
}
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
@@ -289,7 +290,7 @@ func newValue[T ScalarType](val T) (*Value, error) {
if v.ptr == nil {
return nil, errors.New("efl: eina_value_new(CHAR) returned nil")
}
- cv := C.char(scalarToByte(val))
+ cv := C.char(scalarToByte(norm))
if C._ego_value_pset(v.ptr, unsafe.Pointer(&cv)) == 0 {
C._ego_value_free(v.ptr)
return nil, fmt.Errorf("efl: eina_value_pset(CHAR) failed")
@@ -518,16 +519,29 @@ func (v *Value) String() string {
// ---------------------------------------------------------------------------
// Generic conversion helpers
//
-// These bridge the gap between the type-switched concrete C types and the
-// generic T parameter. Because the calling switch statement has already
-// matched the concrete underlying type of T, the assertion from the
-// intermediate concrete type to T is always valid at runtime.
+// newValue switches on the predeclared type predeclared made of its value,
+// so the assertions below always hold.
// ---------------------------------------------------------------------------
-// scalarToInt extracts the int representation of v. v must have underlying
-// type int or int32 — the caller is responsible for the precondition.
-func scalarToInt[T ScalarType](v T) int {
- switch u := any(v).(type) {
+// predeclared returns v as the predeclared type of its kind, so that a named
+// type such as an enum is stored as the Eina type of its underlying type. A
+// value that is already of a predeclared type, or of no scalar kind, is
+// returned as it is, without converting it.
+func predeclared(v any) any {
+ typ := reflect.TypeOf(v)
+ if typ == nil || typ.PkgPath() == "" {
+ return v
+ }
+ if basic, ok := scalarBasicTypes[typ.Kind()]; ok {
+ return reflect.ValueOf(v).Convert(basic).Interface()
+ }
+ return v
+}
+
+// scalarToInt extracts the int representation of v, which must be an int or
+// an int32 — the caller is responsible for the precondition.
+func scalarToInt(v any) int {
+ switch u := v.(type) {
case int:
return u
case int32:
@@ -537,8 +551,8 @@ func scalarToInt[T ScalarType](v T) int {
}
}
-func scalarToUint[T ScalarType](v T) uint {
- switch u := any(v).(type) {
+func scalarToUint(v any) uint {
+ switch u := v.(type) {
case uint:
return u
case uint32:
@@ -548,38 +562,38 @@ func scalarToUint[T ScalarType](v T) uint {
}
}
-func scalarToInt64[T ScalarType](v T) int64 {
- return any(v).(int64) //nolint:forcetypeassert
+func scalarToInt64(v any) int64 {
+ return v.(int64) //nolint:forcetypeassert
}
-func scalarToUint64[T ScalarType](v T) uint64 {
- return any(v).(uint64) //nolint:forcetypeassert
+func scalarToUint64(v any) uint64 {
+ return v.(uint64) //nolint:forcetypeassert
}
-func scalarToInt16[T ScalarType](v T) int16 {
- return any(v).(int16) //nolint:forcetypeassert
+func scalarToInt16(v any) int16 {
+ return v.(int16) //nolint:forcetypeassert
}
-func scalarToUint16[T ScalarType](v T) uint16 {
- return any(v).(uint16) //nolint:forcetypeassert
+func scalarToUint16(v any) uint16 {
+ return v.(uint16) //nolint:forcetypeassert
}
-func scalarToFloat32[T ScalarType](v T) float32 {
- return any(v).(float32) //nolint:forcetypeassert
+func scalarToFloat32(v any) float32 {
+ return v.(float32) //nolint:forcetypeassert
}
-func scalarToFloat64[T ScalarType](v T) float64 {
- return any(v).(float64) //nolint:forcetypeassert
+func scalarToFloat64(v any) float64 {
+ return v.(float64) //nolint:forcetypeassert
}
-func scalarToString[T ScalarType](v T) string {
- return any(v).(string) //nolint:forcetypeassert
+func scalarToString(v any) string {
+ return v.(string) //nolint:forcetypeassert
}
-func scalarToBool[T ScalarType](v T) bool {
- return any(v).(bool) //nolint:forcetypeassert
+func scalarToBool(v any) bool {
+ return v.(bool) //nolint:forcetypeassert
}
-func scalarToByte[T ScalarType](v T) byte {
- return any(v).(byte) //nolint:forcetypeassert
+func scalarToByte(v any) byte {
+ return v.(byte) //nolint:forcetypeassert
}
diff --git a/efl/value_container.go b/efl/value_container.go
index 8be867ee..bef9da5b 100644
--- a/efl/value_container.go
+++ b/efl/value_container.go
@@ -401,7 +401,7 @@ func newValueSlice[T ElementType](vals []T) *Value {
}
// Scalar elements: determine Eina subtype from the zero value of T.
- st := scalarEinaTypeAny(any(zero))
+ st := scalarEinaTypeAny(predeclared(zero))
if st == nil {
return nil
}
@@ -414,7 +414,7 @@ func newValueSlice[T ElementType](vals []T) *Value {
return nil
}
for _, el := range vals {
- if err := appendAnyToArray(raw, any(el)); err != nil {
+ if err := appendAnyToArray(raw, predeclared(el)); err != nil {
C._ego_value_free(raw)
return nil
}
@@ -498,7 +498,7 @@ func newValueMap[T ElementType](vals map[string]T) *Value {
}
// Scalar path.
- st := scalarEinaTypeAny(any(zero))
+ st := scalarEinaTypeAny(predeclared(zero))
if st == nil {
return nil
}
@@ -511,7 +511,7 @@ func newValueMap[T ElementType](vals map[string]T) *Value {
return nil
}
for k, el := range vals {
- if err := setAnyInHash(raw, k, any(el)); err != nil {
+ if err := setAnyInHash(raw, k, predeclared(el)); err != nil {
C._ego_value_free(raw)
return nil
}
diff --git a/efl/value_named_test.go b/efl/value_named_test.go
new file mode 100644
index 00000000..c7fcb98a
--- /dev/null
+++ b/efl/value_named_test.go
@@ -0,0 +1,147 @@
+package efl_test
+
+import (
+ "testing"
+
+ "git.enlightenment.org/cedric/ego/efl"
+)
+
+type mode int
+type flags uint32
+type title string
+type enabled bool
+type ratio float64
+type level byte
+type small int16
+type wide uint64
+type count uint
+type code int32
+type offset int64
+type port uint16
+type gain float32
+
+// valueRoundTrip stores want in a Value, and checks the Value has kind and
+// reads back as want.
+func valueRoundTrip[T efl.ScalarType](t *testing.T, want T, kind efl.ValueKind) {
+ t.Helper()
+ v, err := efl.NewValue(want)
+ if err != nil {
+ t.Errorf("NewValue(%T(%v)): %v", want, want, err)
+ return
+ }
+ defer v.Free()
+ if got := v.Kind(); got != kind {
+ t.Errorf("%T: kind %v, want %v", want, got, kind)
+ }
+ got, err := efl.ValueGet[T](v)
+ if err != nil || got != want {
+ t.Errorf("%T: ValueGet gave %v, %v; want %v", want, got, err, want)
+ }
+}
+
+// A named scalar type is stored as its underlying type.
+func TestNewValueNamedScalars(t *testing.T) {
+ valueRoundTrip(t, mode(2), efl.ValueKindInt)
+ valueRoundTrip(t, flags(7), efl.ValueKindUint)
+ valueRoundTrip(t, title("kitchen"), efl.ValueKindString)
+ valueRoundTrip(t, enabled(true), efl.ValueKindBool)
+ valueRoundTrip(t, ratio(0.5), efl.ValueKindFloat64)
+ valueRoundTrip(t, level(200), efl.ValueKindByte)
+ valueRoundTrip(t, small(-3), efl.ValueKindInt16)
+ valueRoundTrip(t, wide(1<<40), efl.ValueKindUint64)
+ valueRoundTrip(t, count(9), efl.ValueKindUint)
+ valueRoundTrip(t, code(-7), efl.ValueKindInt)
+ valueRoundTrip(t, offset(-1<<40), efl.ValueKindInt64)
+ valueRoundTrip(t, port(8080), efl.ValueKindUint16)
+ valueRoundTrip(t, gain(1.5), efl.ValueKindFloat32)
+}
+
+// A named scalar is the same Value as its underlying type.
+func TestNewValueNamedMatchesUnderlying(t *testing.T) {
+ named, err := efl.NewValue(mode(2))
+ if err != nil {
+ t.Fatalf("NewValue(mode): %v", err)
+ }
+ defer named.Free()
+ plain, err := efl.NewValue(2)
+ if err != nil {
+ t.Fatalf("NewValue(int): %v", err)
+ }
+ defer plain.Free()
+ if named.String() != plain.String() || named.Kind() != plain.Kind() {
+ t.Errorf("mode(2) is %v (%v), int 2 is %v (%v)", named, named.Kind(), plain, plain.Kind())
+ }
+}
+
+// A slice of a named scalar type is an array of its underlying type.
+func TestNewValueSliceOfNamedScalars(t *testing.T) {
+ modes := efl.NewValueSlice([]mode{1, 2, 3})
+ if modes == nil {
+ t.Fatal("NewValueSlice([]mode) gave nil")
+ }
+ defer modes.Free()
+ if got, err := efl.ValueSlice[mode](modes); err != nil || len(got) != 3 || got[2] != 3 {
+ t.Errorf("ValueSlice[mode]: %v, %v", got, err)
+ }
+ if got, err := efl.ValueSlice[int](modes); err != nil || len(got) != 3 || got[1] != 2 {
+ t.Errorf("ValueSlice[int] of modes: %v, %v", got, err)
+ }
+
+ titles := efl.NewValueSlice([]title{"a", "b"})
+ if titles == nil {
+ t.Fatal("NewValueSlice([]title) gave nil")
+ }
+ defer titles.Free()
+ if got, err := efl.ValueSlice[title](titles); err != nil || len(got) != 2 || got[1] != "b" {
+ t.Errorf("ValueSlice[title]: %v, %v", got, err)
+ }
+
+ levels := efl.NewValueSlice([]level{1, 255})
+ if levels == nil {
+ t.Fatal("NewValueSlice([]level) gave nil")
+ }
+ defer levels.Free()
+ if got, err := efl.ValueSlice[byte](levels); err != nil || len(got) != 2 || got[1] != 255 {
+ t.Errorf("ValueSlice[byte] of levels: %v, %v", got, err)
+ }
+}
+
+// A map of a named scalar type is a hash of its underlying type.
+func TestNewValueMapOfNamedScalars(t *testing.T) {
+ modes := efl.NewValueMap(map[string]mode{"a": 1, "b": 2})
+ if modes == nil {
+ t.Fatal("NewValueMap(map[string]mode) gave nil")
+ }
+ defer modes.Free()
+ if got, err := efl.ValueMap[mode](modes); err != nil || len(got) != 2 || got["b"] != 2 {
+ t.Errorf("ValueMap[mode]: %v, %v", got, err)
+ }
+ if got, err := efl.ValueMap[int](modes); err != nil || got["a"] != 1 {
+ t.Errorf("ValueMap[int] of modes: %v, %v", got, err)
+ }
+
+ ratios := efl.NewValueMap(map[string]ratio{"x": 0.25})
+ if ratios == nil {
+ t.Fatal("NewValueMap(map[string]ratio) gave nil")
+ }
+ defer ratios.Free()
+ if got, err := efl.ValueMap[ratio](ratios); err != nil || got["x"] != 0.25 {
+ t.Errorf("ValueMap[ratio]: %v, %v", got, err)
+ }
+}
+
+// Marshalling named scalars, alone and in containers, keeps working.
+func TestValueMarshalNamedScalars(t *testing.T) {
+ var gotMode mode
+ if err := efl.ValueUnmarshal(marshal(t, mode(4)), &gotMode); err != nil || gotMode != 4 {
+ t.Errorf("mode: %v, %v", gotMode, err)
+ }
+ var gotModes []mode
+ if err := efl.ValueUnmarshal(marshal(t, []mode{1, 2}), &gotModes); err != nil || len(gotModes) != 2 || gotModes[1] != 2 {
+ t.Errorf("[]mode: %v, %v", gotModes, err)
+ }
+ var byName map[string]flags
+ if err := efl.ValueUnmarshal(marshal(t, map[string]flags{"a": 9}), &byName); err != nil || byName["a"] != 9 {
+ t.Errorf("map[string]flags: %v, %v", byName, err)
+ }
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.