-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr.go
79 lines (70 loc) · 1.83 KB
/
attr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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"
)
func attrKind(kind C.cue_attr_kind) cue.AttrKind {
switch kind {
case C.CUE_ATTR_FIELD:
return cue.FieldAttr
case C.CUE_ATTR_DECL:
return cue.DeclAttr
case C.CUE_ATTR_VALUE:
return cue.ValueAttr
}
panic("unreachable")
}
//export cue_attrs
func cue_attrs(v C.cue_value, kind C.cue_attr_kind, count *C.size_t) *C.cue_attr {
attrs := cueValue(v).Attributes(attrKind(kind))
*count = C.size_t(len(attrs))
if len(attrs) == 0 {
return nil
}
s, ptr := calloc[C.cue_attr](len(attrs), C.sizeof_cue_attr)
for i, a := range attrs {
s[i] = cueAttrHandle(a)
}
return ptr
}
//export cue_attr_name
func cue_attr_name(a C.cue_attr) *C.char {
attr := cueAttr(a)
return C.CString(attr.Name())
}
//export cue_attr_value
func cue_attr_value(a C.cue_attr) *C.char {
attr := cueAttr(a)
return C.CString(attr.Contents())
}
//export cue_attr_numargs
func cue_attr_numargs(a C.cue_attr) C.size_t {
attr := cueAttr(a)
return C.size_t(attr.NumArgs())
}
//export cue_attr_getarg
func cue_attr_getarg(a C.cue_attr, n C.size_t, arg *C.cue_attr_arg) {
attr := cueAttr(a)
k, v := attr.Arg(int(n))
arg.key = C.CString(k)
arg.val = C.CString(v)
return
}