Skip to content

Commit 56c0c63

Browse files
Merge pull request #80 from nextmv-io/develop
Release v0.19.1
2 parents 726d1b1 + 71200a4 commit 56c0c63

22 files changed

+176
-309
lines changed

.github/workflows/go-build-test.yml

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go build & test
22
on: [push]
33
jobs:
4-
sdk:
4+
sdk-go-build-test:
55
runs-on: ubuntu-latest
66
env:
77
GOPRIVATE: github.com/nextmv-io/*
@@ -59,22 +59,12 @@ jobs:
5959
# replaced for the local sdk repo checked out in this action.
6060
go mod edit -replace=github.com/nextmv-io/sdk=${{env.SDK_PATH}}
6161
62-
# Create the path where the shared object binaries are going to be
63-
# saved.
64-
VERSION=$(go run cmd/version/main.go)
65-
GOVERSION=$(go env GOVERSION)
66-
GOOS=$(go env GOOS)
67-
GOARCH=$(go env GOARCH)
68-
OUT_PATH=${{env.NEXTMV_LIBRARY_PATH}}/nextmv-sdk-$VERSION-$GOVERSION-$GOOS-$GOARCH.so
69-
70-
# Build without -trimpath because the sdk dependency is being
71-
# replaced for the local sdk repo used in the action.
72-
cd plugins/sdk
73-
go build -buildmode plugin -o $OUT_PATH
62+
# Build the plugins.
63+
REMOVE_TRIMPATH=1 NEXTMV_LIBRARY_PATH=${{ env.NEXTMV_LIBRARY_PATH }} bash scripts/build.sh
7464
working-directory: ${{env.RESOURCES}}
7565

7666
- name: go build
7767
run: go build -v ./...
7868

7969
- name: go test
80-
run: NEXTMV_LIBRARY_PATH=${{ env.NEXTMV_LIBRARY_PATH }} NEXTMV_TOKEN=$(nextmv token) go test -v ./...
70+
run: NEXTMV_LIBRARY_PATH=${{ env.NEXTMV_LIBRARY_PATH }} NEXTMV_TOKEN=$(nextmv token) go test ./...

.github/workflows/go-lint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go lint
22
on: [push]
33
jobs:
4-
sdk:
4+
sdk-go-lint:
55
runs-on: ubuntu-latest
66
steps:
77
- name: set up go

.github/workflows/markdown-lint.yml

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
name: markdown lint
22
on: [push]
33
jobs:
4-
sdk:
4+
sdk-markdown-lint:
55
runs-on: ubuntu-latest
66
steps:
7+
# Checkout the changes
78
- name: git clone
89
uses: actions/checkout@v3
910

10-
- name: set up Node.js
11-
uses: actions/setup-node@v3
11+
# Use markdownlint-cli2
12+
- uses: DavidAnson/markdownlint-cli2-action@v7
1213
with:
13-
node-version: "14"
14-
15-
- name: install markdownlint-cli2
16-
run: |
17-
npm install markdownlint-cli2 --global
18-
19-
- name: run markdownlint-cli2
20-
run: |
21-
markdownlint-cli2 "**/*.md"
14+
globs: "**/*.md"

README.md

+2-13
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@ problems. Please find the following packages:
1010
output.
1111
- [model][model]: modeling components such as integer domains and ranges.
1212

13-
Please visit the official [Go Package Docs][pkgsite] for documentation and
14-
testable examples.
15-
16-
## Usage
17-
18-
To run a decision automation problem with SDK you need the [Nextmv CLI][cli].
19-
20-
## Get started
21-
22-
Please visit the [tour of SDK][tour] to get started with data store modeling.
13+
Please visit the official [Nextmv docs][docs] for comprehensive information.
2314

2415
## Installation
2516

@@ -29,10 +20,8 @@ Nextmv's SDK is meant to be used in Go projects. To download please run:
2920
go get github.com/nextmv-io/sdk
3021
```
3122

32-
[pkgsite]: https://pkg.go.dev/github.com/nextmv-io/sdk
3323
[store]: ./store
3424
[route]: ./route
3525
[run]: ./run
3626
[model]: ./model
37-
[tour]: https://github.com/nextmv-io/tour
38-
[cli]: https://docs.nextmv.io/development/cli
27+
[docs]: https://docs.nextmv.io

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.19.0
1+
v0.19.1

connect/connect.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Package connect provides a Connector which allows to connect method
2+
// definition with their implementations in plugins
3+
package connect
4+
5+
import (
6+
"fmt"
7+
"runtime"
8+
"strings"
9+
"sync"
10+
11+
"github.com/nextmv-io/sdk/plugin"
12+
)
13+
14+
// Connector connects methods with their implementations in plugins.
15+
type Connector struct {
16+
connected map[any]struct{}
17+
mtx sync.Mutex
18+
slug, prefix string
19+
}
20+
21+
// NewConnector creates a new Connector.
22+
func NewConnector(slug, prefix string) *Connector {
23+
return &Connector{
24+
connected: make(map[any]struct{}),
25+
slug: slug,
26+
prefix: prefix,
27+
}
28+
}
29+
30+
// Connect connects a method with its implementation.
31+
func Connect[T any](c *Connector, target *T, suffix ...string) {
32+
if _, ok := c.connected[target]; ok {
33+
return
34+
}
35+
36+
c.mtx.Lock()
37+
defer c.mtx.Unlock()
38+
39+
if _, ok := c.connected[target]; ok {
40+
return
41+
}
42+
43+
// get the calling function, get ok to make linter happy
44+
pc, _, _, ok := runtime.Caller(1)
45+
// we don't actually need ok, so noop
46+
_ = ok
47+
// get name of the calling function
48+
fullName := runtime.FuncForPC(pc).Name()
49+
trimmed := strings.TrimSuffix(fullName, "[...]")
50+
split := strings.Split(trimmed, ".")
51+
name := split[len(split)-1]
52+
53+
// for methods that share more mapped names, such as NewMap, we use a suffix
54+
if len(suffix) == 1 {
55+
name += suffix[0]
56+
}
57+
58+
// connect by convention
59+
plugin.Connect(c.slug, fmt.Sprintf("%s%s", c.prefix, name), target)
60+
c.connected[target] = struct{}{}
61+
}

mip/connect.go

-31
This file was deleted.

mip/example_objective_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ func ExampleObjective_terms() {
5050
fmt.Println(t3.Var().Index())
5151
fmt.Println(t3.Coefficient())
5252

53-
fmt.Println(len(model.Objective().Terms()))
54-
fmt.Println(model.Objective().Terms()[0].Coefficient())
55-
fmt.Println(model.Objective().IsMaximize())
56-
fmt.Println(model.Objective().Terms()[0])
57-
fmt.Println(model.Objective().Terms()[1])
58-
// Output:
53+
terms := model.Objective().Terms()
54+
fmt.Println(len(terms))
55+
for _, term := range terms {
56+
fmt.Println(term.Var(), term.Coefficient())
57+
}
58+
fmt.Println("isMaximize: ", model.Objective().IsMaximize())
59+
// Unordered output:
5960
// 0
6061
// 2 B0
6162
// 1 B0
@@ -67,10 +68,9 @@ func ExampleObjective_terms() {
6768
// 1
6869
// 3
6970
// 2
70-
// 3
71-
// false
72-
// 3 B0
73-
// 3 B1
71+
// B0 3
72+
// B1 3
73+
// isMaximize: false
7474
}
7575

7676
func benchmarkObjectiveNewTerms(nrTerms int, b *testing.B) {

mip/mip.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package mip
22

3+
import "github.com/nextmv-io/sdk/connect"
4+
35
// NewSolveOptions returns default solver options.
46
func NewSolveOptions() SolveOptions {
5-
connect()
7+
connect.Connect(con, &newSolveOptions)
68
return newSolveOptions()
79
}
810

911
// NewModel creates an empty MIP model.
1012
func NewModel() Model {
11-
connect()
13+
connect.Connect(con, &newModel)
1214
return newModel()
1315
}
1416

1517
// NewSolver returns a new Solver implemented by the given provider.
1618
func NewSolver(provider SolverProvider, model Model) (Solver, error) {
17-
connect()
19+
connect.Connect(con, &model)
1820
return newSolver(provider, model)
1921
}
2022

2123
var (
24+
con = connect.NewConnector("sdk", "MIP")
2225
newSolveOptions func() SolveOptions
2326
newSolver func(SolverProvider, Model) (Solver, error)
2427
newModel func() Model

model/connect.go

-39
This file was deleted.

model/model.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package model
22

3-
import "math/bits"
3+
import (
4+
"math/bits"
5+
6+
"github.com/nextmv-io/sdk/connect"
7+
)
48

59
// Constants for integer bounds.
610
const (
@@ -108,41 +112,42 @@ type Iterator interface {
108112

109113
// NewDomain creates a domain of integers.
110114
func NewDomain(ranges ...Range) Domain {
111-
connect()
115+
connect.Connect(con, &newDomainFunc)
112116
return newDomainFunc(ranges...)
113117
}
114118

115119
// Singleton creates a domain containing one integer value.
116120
func Singleton(value int) Domain {
117-
connect()
121+
connect.Connect(con, &singletonFunc)
118122
return singletonFunc(value)
119123
}
120124

121125
// Multiple creates a domain containing multiple integer values.
122126
func Multiple(values ...int) Domain {
123-
connect()
127+
connect.Connect(con, &multipleFunc)
124128
return multipleFunc(values...)
125129
}
126130

127131
// NewDomains creates a sequence of domains.
128132
func NewDomains(domains ...Domain) Domains {
129-
connect()
133+
connect.Connect(con, &newDomainsFunc)
130134
return newDomainsFunc(domains...)
131135
}
132136

133137
// Repeat a domain n times.
134138
func Repeat(n int, d Domain) Domains {
135-
connect()
139+
connect.Connect(con, &repeatFunc)
136140
return repeatFunc(n, d)
137141
}
138142

139143
// NewRange create a new integer range.
140144
func NewRange(min, max int) Range {
141-
connect()
145+
connect.Connect(con, &newRangeFunc)
142146
return newRangeFunc(min, max)
143147
}
144148

145149
var (
150+
con = connect.NewConnector("sdk", "Model")
146151
newDomainFunc func(...Range) Domain
147152
singletonFunc func(int) Domain
148153
multipleFunc func(...int) Domain

0 commit comments

Comments
 (0)