1
0
mirror of https://github.com/deuill/go-php.git synced 2024-09-21 00:40:45 +00:00
go-php/value.go

53 lines
964 B
Go

package php
// #include <stdlib.h>
// #include <stdbool.h>
// #include "value.h"
import "C"
import (
"fmt"
"unsafe"
)
type Value struct {
value unsafe.Pointer
}
func (v *Value) Ptr() unsafe.Pointer {
return v.value
}
func (v *Value) Destroy() {
C.value_destroy(v.value)
v = nil
}
func NewValue(v interface{}) (*Value, error) {
var ptr unsafe.Pointer
var err error
// Determine value type and create PHP value from the concrete type.
switch v := v.(type) {
case int:
ptr, err = C.value_create_long(C.long(v))
case float64:
ptr, err = C.value_create_double(C.double(v))
case bool:
ptr, err = C.value_create_bool(C.bool(v))
case string:
str := C.CString(v)
defer C.free(unsafe.Pointer(str))
ptr, err = C.value_create_string(str)
default:
return nil, fmt.Errorf("Cannot create value of unknown type '%T'", v)
}
if err != nil {
return nil, fmt.Errorf("Creating value from '%v' failed", v)
}
return &Value{value: ptr}, nil
}