diff --git a/.gitignore b/.gitignore index 370080f..8dceba4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ /libcue.h /libcue.so /libcue.a + +/.idea/ \ No newline at end of file diff --git a/cue.h b/cue.h index b1a938f..4213142 100644 --- a/cue.h +++ b/cue.h @@ -101,6 +101,8 @@ char* cue_error_string(cue_error); cue_error cue_compile_string(cue_ctx, char*, cue_bopt*, cue_value*); cue_error cue_compile_bytes(cue_ctx, void*, size_t, cue_bopt*, cue_value*); +cue_value* cue_fields(cue_value, size_t*); +cue_value* cue_list(cue_value, size_t*); cue_value cue_top(cue_ctx); cue_value cue_bottom(cue_ctx); cue_value cue_unify(cue_value, cue_value); @@ -112,6 +114,7 @@ cue_value cue_from_bool(cue_ctx, bool); cue_value cue_from_double(cue_ctx, double); cue_value cue_from_string(cue_ctx, char*); cue_value cue_from_bytes(cue_ctx, void*, size_t); +cue_value cue_from_list(cue_ctx, cue_value*, size_t); cue_error cue_dec_int64(cue_value, int64_t*); cue_error cue_dec_uint64(cue_value, uint64_t*); cue_error cue_dec_bool(cue_value, bool*); @@ -124,6 +127,7 @@ cue_value cue_default(cue_value, bool*); cue_kind cue_concrete_kind(cue_value); cue_kind cue_incomplete_kind(cue_value); cue_error cue_value_error(cue_value); +char* cue_path(cue_value); bool cue_is_equal(cue_value, cue_value); cue_bopt cue_filename(char*); diff --git a/fields.go b/fields.go new file mode 100644 index 0000000..7d08f5f --- /dev/null +++ b/fields.go @@ -0,0 +1,52 @@ +// Copyright 2024 The CUE Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +/* +#include +#include +#include "cue.h" +*/ +import "C" + +import ( + "cuelang.org/go/cue" +) + +//export cue_fields +func cue_fields(v C.cue_value, count *C.size_t) *C.cue_value { + iter, err := cueValue(v).Fields() + if err != nil { + return nil + } + + var fields []cue.Value + for iter.Next() { + fields = append(fields, iter.Value()) + } + + *count = C.size_t(len(fields)) + + if len(fields) == 0 { + return nil + } + + s, ptr := calloc[C.cue_value](len(fields), C.sizeof_cue_value) + for i, f := range fields { + s[i] = cueValueHandle(f) + } + + return ptr +} diff --git a/value.go b/value.go index e558fd6..906359b 100644 --- a/value.go +++ b/value.go @@ -125,6 +125,16 @@ func cue_from_bytes(ctx C.cue_ctx, buf unsafe.Pointer, len C.size_t) C.cue_value return cueValueHandle(val) } +//export cue_from_list +func cue_from_list(ctx C.cue_ctx, values *C.cue_value, count C.size_t) C.cue_value { + var v []cue.Value + for _, val := range unsafe.Slice(values, count) { + v = append(v, cueValue(val)) + } + nl := cueContext(ctx).NewList(v...) + return cueValueHandle(nl) +} + //export cue_dec_int64 func cue_dec_int64(v C.cue_value, res *C.int64_t) C.cue_error { n, err := cueValue(v).Int64() @@ -286,3 +296,35 @@ func cue_value_error(v C.cue_value) C.cue_error { } return 0 } + +//export cue_path +func cue_path(v C.cue_value) *C.char { + label := cueValue(v).Path().String() + return C.CString(label) +} + +//export cue_list +func cue_list(v C.cue_value, count *C.size_t) *C.cue_value { + iter, err := cueValue(v).List() + if err != nil { + return nil + } + + var elements []cue.Value + for iter.Next() { + elements = append(elements, iter.Value()) + } + + *count = C.size_t(len(elements)) + + if len(elements) == 0 { + return nil + } + + s, ptr := calloc[C.cue_value](len(elements), C.sizeof_cue_value) + for i, elem := range elements { + s[i] = cueValueHandle(elem) + } + + return ptr +}