Skip to content

Commit 40e20e6

Browse files
committed
reapi: use enum for go bindings match allocate
Problem: golang bindings for match allocate use strings to pass in match options instead of using enum. use enum match_op_t go go bindings. Change cgo include path to be the base directory import of match_op.h.
1 parent 7ee2c6c commit 40e20e6

File tree

7 files changed

+25
-43
lines changed

7 files changed

+25
-43
lines changed

resource/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ set(RESOURCE_HEADERS
2222
traversers/dfu_impl.hpp
2323
policies/base/dfu_match_cb.hpp
2424
policies/base/matcher.hpp
25-
policies/base/match_op.h
2625
readers/resource_namespace_remapper.hpp
2726
readers/resource_reader_base.hpp
2827
readers/resource_spec_grug.hpp

resource/reapi/bindings/go/src/fluxcli/reapi_cli.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,14 @@
1111
package fluxcli
1212

1313
/*
14-
#include "reapi_cli.h"
14+
#include "resource/reapi/bindings/c/reapi_cli.h"
1515
*/
1616
import "C"
1717
import (
1818
"fmt"
1919
"unsafe"
20-
)
2120

22-
const (
23-
matchAllocate = "allocate"
24-
matchAllocateOrelseReserve = "allocate_orelse_reserve"
25-
matchAllocateWSatisfiability = "allocate_with_satisfiability"
26-
matchSatisfiability = "satisfiability"
21+
"github.com/flux-framework/flux-sched/resource/reapi/bindings/go/src/pkg/types"
2722
)
2823

2924
type (
@@ -105,14 +100,14 @@ func (cli *ReapiClient) InitContext(jgf string, options string) (err error) {
105100
//
106101
// \return 0 on success; -1 on error.
107102
func (cli *ReapiClient) Match(
108-
match_op string,
103+
match_op types.MatchType,
109104
jobspec string,
110105
) (reserved bool, allocated string, at int64, overhead float64, jobid uint64, err error) {
111106
var r = C.CString("")
112107
spec := C.CString(jobspec)
113108

114109
fluxerr := (int)(C.reapi_cli_match((*C.struct_reapi_cli_ctx)(cli.ctx),
115-
(C.CString)(match_op),
110+
C.match_op_t(match_op),
116111
spec,
117112
(*C.ulong)(&jobid),
118113
(*C.bool)(&reserved),
@@ -154,12 +149,12 @@ func (cli *ReapiClient) MatchAllocate(
154149
orelse_reserve bool,
155150
jobspec string,
156151
) (reserved bool, allocated string, at int64, overhead float64, jobid uint64, err error) {
157-
var match_op string
152+
var match_op types.MatchType
158153

159154
if orelse_reserve {
160-
match_op = matchAllocateOrelseReserve
155+
match_op = types.MatchAllocateOrElseReserve
161156
} else {
162-
match_op = matchAllocate
157+
match_op = types.MatchAllocate
163158
}
164159

165160
return cli.Match(match_op, jobspec)

resource/reapi/bindings/go/src/fluxmodule/reapi_module.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@
1010

1111
package fluxcli
1212

13-
// #include "reapi_module.h"
13+
// #include "resource/reapi/bindings/c/reapi_module.h"
1414
import "C"
1515
import (
1616
"fmt"
1717
"unsafe"
18-
)
1918

20-
const (
21-
matchAllocate = "allocate"
22-
matchAllocateOrelseReserve = "allocate_orelse_reserve"
23-
matchAllocateWSatisfiability = "allocate_with_satisfiability"
24-
matchSatisfiability = "satisfiability"
19+
"github.com/flux-framework/flux-sched/resource/reapi/bindings/go/src/pkg/types"
2520
)
2621

2722
type (
@@ -62,7 +57,7 @@ func (m *ReapiModule) Destroy() {
6257
// int reapi_module_match_allocate (reapi_module_ctx_t *ctx, char *match_op,
6358
// at: is the scheduled time "at"
6459
func (m *ReapiModule) Match(
65-
match_op string,
60+
match_op types.MatchType,
6661
jobspec string,
6762
jobid int,
6863
) (reserved bool, allocated string, at int64, overhead float64, err error) {
@@ -73,7 +68,7 @@ func (m *ReapiModule) Match(
7368
spec := C.CString(jobspec)
7469

7570
fluxerr := (int)(C.reapi_module_match((*C.struct_reapi_module_ctx)(m.ctx),
76-
(C.CString)(match_op),
71+
C.match_op_t(match_op),
7772
spec,
7873
(C.ulong)(jobid),
7974
(*C.bool)(&reserved),
@@ -99,9 +94,9 @@ func (m *ReapiModule) MatchAllocate(
9994
var match_op string
10095

10196
if orelse_reserve {
102-
match_op = matchAllocateOrelseReserve
97+
match_op =types.MatchAllocateOrElseReserve
10398
} else {
104-
match_op = matchAllocate
99+
match_op = types.MatchAllocate
105100
}
106101

107102
return m.Match(match_op, jobspec, jobid)
@@ -112,7 +107,7 @@ func (m *ReapiModule) MatchSatisfy(
112107
jobspec string,
113108
jobid int,
114109
) (reserved bool, allocated string, at int64, overhead float64, err error) {
115-
match_op := matchSatisfiability
110+
match_op := types.MatchSatisfiability
116111
return m.Match(match_op, jobspec, jobid)
117112
}
118113

resource/reapi/bindings/go/src/pkg/types/types.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,23 @@ package types
1111
\*****************************************************************************/
1212

1313
/*
14-
#include "reapi_cli.h"
15-
16-
typedef enum {
17-
MATCH_UNKNOWN = 0
18-
MATCH_ALLOCATE = 1
19-
MATCH_ALLOCATE_ORELSE_RESERVE = 2
20-
MATCH_ALLOCATE_W_SATISFIABILITY = 3
21-
MATCH_SATISFIABILITY = 4
22-
} match_op_enum;
14+
#include "resource/reapi/bindings/c/reapi_cli.h"
2315
2416
/* usage:
2517
mt := MatchType
26-
C.match_op_enum(mt)
18+
C.match_op_t(mt)
2719
*/
2820

2921
import "C"
3022

3123
type MatchType int
3224

33-
// MatchUnknown serves as a sentinal value that it's undefined
25+
// MatchUnknown serves as a sentinel value that it's undefined
3426
const (
3527
MatchUnknown MatchType = iota // unknown
3628
MatchAllocate // allocate
37-
MatchAllocateOrElseReserve // allocate or else reserve
3829
MatchAllocateWithSatisfiability // allocate with satisfiability
30+
MatchAllocateOrElseReserve // allocate or else reserve
3931
MatchSatisfiability // satisfiability
4032
)
4133

resource/reapi/bindings/go/src/pkg/types/types_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func TestAsInt(t *testing.T) {
5959
{description: "unknown", input: MatchUnknown, expected: 0},
6060
{description: "allocate", input: MatchAllocate, expected: 1},
6161
{description: "satisfiability", input: MatchSatisfiability, expected: 4},
62-
{description: "allocate or else reserve", input: MatchAllocateOrElseReserve, expected: 2},
63-
{description: "allocate with satisfiability", input: MatchAllocateWithSatisfiability, expected: 3},
62+
{description: "allocate or else reserve", input: MatchAllocateOrElseReserve, expected: 3},
63+
{description: "allocate with satisfiability", input: MatchAllocateWithSatisfiability, expected: 2},
6464
}
6565
for _, item := range tests {
6666
t.Run(item.description, func(t *testing.T) {

resource/reapi/bindings/go/src/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set(TARGET main)
22
set(SRCS main.go)
3-
set(CGO_CFLAGS "-I${CMAKE_BINARY_DIR}/resource/reapi/bindings/c")
3+
set(CGO_CFLAGS "-I${CMAKE_BINARY_DIR}")
44

55
# This is currently passed but not used because when passed into add_custom_command the spaces are escaped
66
set(CGO_LIBRARY_FLAGS -Wl,-R

resource/reapi/bindings/go/src/test/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os"
1717

1818
"github.com/flux-framework/flux-sched/resource/reapi/bindings/go/src/fluxcli"
19+
"github.com/flux-framework/flux-sched/resource/reapi/bindings/go/src/pkg/types"
1920
)
2021

2122
func main() {
@@ -61,7 +62,7 @@ func main() {
6162
}
6263
printOutput(reserved, allocated, at, jobid, err)
6364

64-
reserved, allocated, at, overhead, jobid, err = cli.Match("allocate", string(jobspec))
65+
reserved, allocated, at, overhead, jobid, err = cli.Match(types.MatchAllocate, string(jobspec))
6566
fmt.Println("Errors so far: \n", cli.GetErrMsg())
6667

6768
if err != nil {
@@ -70,7 +71,7 @@ func main() {
7071
}
7172
printOutput(reserved, allocated, at, jobid, err)
7273

73-
reserved, allocated, at, overhead, jobid, err = cli.Match("allocate_orelse_reserve", string(jobspec))
74+
reserved, allocated, at, overhead, jobid, err = cli.Match(types.MatchAllocateOrElseReserve, string(jobspec))
7475
fmt.Println("Errors so far: \n", cli.GetErrMsg())
7576

7677
if err != nil {
@@ -79,7 +80,7 @@ func main() {
7980
}
8081
printOutput(reserved, allocated, at, jobid, err)
8182

82-
reserved, allocated, at, overhead, jobid, err = cli.Match("allocate_with_satisfiability", string(jobspec))
83+
reserved, allocated, at, overhead, jobid, err = cli.Match(types.MatchAllocateWithSatisfiability, string(jobspec))
8384
fmt.Println("Errors so far: \n", cli.GetErrMsg())
8485

8586
if err != nil {

0 commit comments

Comments
 (0)