Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
/libcue.h
/libcue.so
/libcue.a

/.idea/
4 changes: 4 additions & 0 deletions cue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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*);
Expand All @@ -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*);
Expand Down
52 changes: 52 additions & 0 deletions fields.go
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <stdbool.h>
#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
}
42 changes: 42 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
}