Skip to content

Commit e7fb2bf

Browse files
authored
Add simple converter (#3)
* feat: add simple converter from new and helper functions Problem: we should be able to create a new jobspec easily, and provide helper functions to read/serialize and validate. Solution: do that. Signed-off-by: vsoch <vsoch@users.noreply.github.com>
1 parent 0073278 commit e7fb2bf

File tree

12 files changed

+270
-84
lines changed

12 files changed

+270
-84
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ COMMONENVVAR=GOOS=$(shell uname -s | tr A-Z a-z)
33
RELEASE_VERSION?=v$(shell date +%Y%m%d)-$(shell git describe --tags --match "v*")
44

55
.PHONY: all
6-
all: example1 example2 example3 example4 example5 example6
6+
all: example1 example2 example3 example4 example5 example6 createnew
77

88
.PHONY: build
99
build:
1010
go mod tidy
1111
mkdir -p ./examples/v1/bin
1212

1313
# Build examples
14+
.PHONY: createnew
15+
createnew: build
16+
$(COMMONENVVAR) $(BUILDENVVAR) go build -ldflags '-w' -o ./examples/v1/bin/createnew examples/v1/createnew/example.go
17+
1418
.PHONY: example1
1519
example1: build
1620
$(COMMONENVVAR) $(BUILDENVVAR) go build -ldflags '-w' -o ./examples/v1/bin/example1 examples/v1/example1/example.go
@@ -43,6 +47,7 @@ test:
4347
./examples/v1/bin/example4
4448
./examples/v1/bin/example5
4549
./examples/v1/bin/example6
50+
./examples/v1/bin/createnew
4651

4752
.PHONY: clean
4853
clean:

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,45 @@ Run all tests at once:
1616
make test
1717
```
1818

19+
Here is an example of usage. Note that this isn't a full program, but is intended to show helper functions.
20+
In this small program, we load in a jobspec (from yaml) and then validate and serialize to each of json and yaml.
21+
22+
```go
23+
package main
24+
25+
import (
26+
"fmt"
27+
"os"
28+
29+
v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
30+
)
31+
32+
func main() {
33+
34+
// Example 1: reading from file
35+
yamlFile := "examples/v1/example1/jobspec.yaml"
36+
37+
// This is how to read from a YAML file
38+
js, err := v1.LoadJobspecYaml(yamlFile)
39+
// Validate the jobspec
40+
valid, err := js.Validate()
41+
42+
// Convert back to YAML (print out as string(out))
43+
out, err := js.JobspecToYaml()
44+
45+
// Convert back into JSON (also print string(out))
46+
out, err = js.JobspecToJson()
47+
48+
// Example 2: creating from scratch
49+
var nodes int32 = 2
50+
var tasks int32 = 12
51+
js, err := v1.NewSimpleJobspec("myjobspec", "echo hello world", nodes, tasks)
52+
// proceed with equivalent functions above!
53+
}
54+
```
55+
56+
For full examples, see the [examples](examples/v1) directory.
57+
1958
### Version 1
2059

2160
You can run any example (and view the code) to see how it works!

examples/v1/createnew/example.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
8+
)
9+
10+
func main() {
11+
fmt.Println("This example creates a new Jobspec")
12+
13+
var nodes int32 = 2
14+
var tasks int32 = 12
15+
js, err := v1.NewSimpleJobspec("myjobspec", "echo hello world", nodes, tasks)
16+
if err != nil {
17+
fmt.Printf("error creating jobspec: %s\n", err)
18+
os.Exit(1)
19+
}
20+
21+
// Validate the jobspec
22+
valid, err := js.Validate()
23+
if !valid || err != nil {
24+
fmt.Printf("schema is not valid:%s\n", err)
25+
os.Exit(1)
26+
} else {
27+
fmt.Println("schema is valid")
28+
}
29+
fmt.Println(js)
30+
31+
out, err := js.JobspecToYaml()
32+
if err != nil {
33+
fmt.Printf("error marshalling jobspec: %s\n", err)
34+
os.Exit(1)
35+
}
36+
fmt.Println(string(out))
37+
38+
// One example of json
39+
out, err = js.JobspecToJson()
40+
if err != nil {
41+
fmt.Printf("error marshalling jobspec: %s\n", err)
42+
os.Exit(1)
43+
}
44+
fmt.Println(string(out))
45+
}

examples/v1/example1/example.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"fmt"
66
"os"
77

8-
"sigs.k8s.io/yaml"
9-
10-
"github.com/compspec/jobspec-go/pkg/schema"
11-
128
v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
139
)
1410

@@ -24,30 +20,31 @@ func main() {
2420
flag.Usage()
2521
os.Exit(0)
2622
}
27-
file, err := os.ReadFile(yamlFile)
23+
js, err := v1.LoadJobspecYaml(yamlFile)
2824
if err != nil {
2925
fmt.Printf("error reading %s:%s\n", yamlFile, err)
3026
os.Exit(1)
3127
}
3228

3329
// Validate the jobspec
34-
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
30+
valid, err := js.Validate()
3531
if !valid || err != nil {
3632
fmt.Printf("schema is not valid:%s\n", err)
3733
os.Exit(1)
3834
} else {
3935
fmt.Println("schema is valid")
4036
}
37+
fmt.Println(js)
4138

42-
js := v1.Jobspec{}
43-
err = yaml.Unmarshal([]byte(file), &js)
39+
out, err := js.JobspecToYaml()
4440
if err != nil {
45-
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
41+
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
4642
os.Exit(1)
4743
}
48-
fmt.Println(js)
44+
fmt.Println(string(out))
4945

50-
out, err := yaml.Marshal(js)
46+
// One example of json
47+
out, err = js.JobspecToJson()
5148
if err != nil {
5249
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
5350
os.Exit(1)

examples/v1/example2/example.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"fmt"
66
"os"
77

8-
"sigs.k8s.io/yaml"
9-
10-
"github.com/compspec/jobspec-go/pkg/schema"
11-
128
v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
139
)
1410

@@ -24,30 +20,23 @@ func main() {
2420
flag.Usage()
2521
os.Exit(0)
2622
}
27-
file, err := os.ReadFile(yamlFile)
23+
js, err := v1.LoadJobspecYaml(yamlFile)
2824
if err != nil {
2925
fmt.Printf("error reading %s:%s\n", yamlFile, err)
3026
os.Exit(1)
3127
}
3228

3329
// Validate the jobspec
34-
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
30+
valid, err := js.Validate()
3531
if !valid || err != nil {
3632
fmt.Printf("schema is not valid:%s\n", err)
3733
os.Exit(1)
3834
} else {
3935
fmt.Println("schema is valid")
4036
}
41-
42-
js := v1.Jobspec{}
43-
err = yaml.Unmarshal([]byte(file), &js)
44-
if err != nil {
45-
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
46-
os.Exit(1)
47-
}
4837
fmt.Println(js)
4938

50-
out, err := yaml.Marshal(js)
39+
out, err := js.JobspecToYaml()
5140
if err != nil {
5241
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
5342
os.Exit(1)

examples/v1/example3/example.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"fmt"
66
"os"
77

8-
"sigs.k8s.io/yaml"
9-
10-
"github.com/compspec/jobspec-go/pkg/schema"
11-
128
v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
139
)
1410

@@ -24,33 +20,27 @@ func main() {
2420
flag.Usage()
2521
os.Exit(0)
2622
}
27-
file, err := os.ReadFile(yamlFile)
23+
js, err := v1.LoadJobspecYaml(yamlFile)
2824
if err != nil {
2925
fmt.Printf("error reading %s:%s\n", yamlFile, err)
3026
os.Exit(1)
3127
}
3228

3329
// Validate the jobspec
34-
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
30+
valid, err := js.Validate()
3531
if !valid || err != nil {
3632
fmt.Printf("schema is not valid:%s\n", err)
3733
os.Exit(1)
3834
} else {
3935
fmt.Println("schema is valid")
4036
}
41-
42-
js := v1.Jobspec{}
43-
err = yaml.Unmarshal([]byte(file), &js)
44-
if err != nil {
45-
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
46-
os.Exit(1)
47-
}
4837
fmt.Println(js)
4938

50-
out, err := yaml.Marshal(js)
39+
out, err := js.JobspecToYaml()
5140
if err != nil {
5241
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
5342
os.Exit(1)
5443
}
5544
fmt.Println(string(out))
45+
5646
}

examples/v1/example4/example.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"fmt"
66
"os"
77

8-
"sigs.k8s.io/yaml"
9-
10-
"github.com/compspec/jobspec-go/pkg/schema"
11-
128
v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
139
)
1410

@@ -24,34 +20,27 @@ func main() {
2420
flag.Usage()
2521
os.Exit(0)
2622
}
27-
file, err := os.ReadFile(yamlFile)
23+
js, err := v1.LoadJobspecYaml(yamlFile)
2824
if err != nil {
2925
fmt.Printf("error reading %s:%s\n", yamlFile, err)
3026
os.Exit(1)
3127
}
3228

3329
// Validate the jobspec
34-
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
30+
valid, err := js.Validate()
3531
if !valid || err != nil {
3632
fmt.Printf("schema is not valid:%s\n", err)
3733
os.Exit(1)
3834
} else {
3935
fmt.Println("schema is valid")
4036
}
41-
42-
js := v1.Jobspec{}
43-
err = yaml.Unmarshal([]byte(file), &js)
44-
if err != nil {
45-
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
46-
os.Exit(1)
47-
}
4837
fmt.Println(js)
4938

50-
out, err := yaml.Marshal(js)
51-
39+
out, err := js.JobspecToYaml()
5240
if err != nil {
5341
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
5442
os.Exit(1)
5543
}
5644
fmt.Println(string(out))
45+
5746
}

examples/v1/example5/example.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"fmt"
66
"os"
77

8-
"sigs.k8s.io/yaml"
9-
10-
"github.com/compspec/jobspec-go/pkg/schema"
11-
128
v1 "github.com/compspec/jobspec-go/pkg/jobspec/v1"
139
)
1410

@@ -24,33 +20,27 @@ func main() {
2420
flag.Usage()
2521
os.Exit(0)
2622
}
27-
file, err := os.ReadFile(yamlFile)
23+
js, err := v1.LoadJobspecYaml(yamlFile)
2824
if err != nil {
2925
fmt.Printf("error reading %s:%s\n", yamlFile, err)
3026
os.Exit(1)
3127
}
3228

3329
// Validate the jobspec
34-
valid, err := schema.Validate(file, schema.SchemaUrl, v1.Schema)
30+
valid, err := js.Validate()
3531
if !valid || err != nil {
3632
fmt.Printf("schema is not valid:%s\n", err)
3733
os.Exit(1)
3834
} else {
3935
fmt.Println("schema is valid")
4036
}
41-
42-
js := v1.Jobspec{}
43-
err = yaml.Unmarshal([]byte(file), &js)
44-
if err != nil {
45-
fmt.Printf("error unmarshalling %s:%s\n", yamlFile, err)
46-
os.Exit(1)
47-
}
4837
fmt.Println(js)
4938

50-
out, err := yaml.Marshal(js)
39+
out, err := js.JobspecToYaml()
5140
if err != nil {
5241
fmt.Printf("error marshalling %s:%s\n", yamlFile, err)
5342
os.Exit(1)
5443
}
5544
fmt.Println(string(out))
45+
5646
}

0 commit comments

Comments
 (0)