Skip to content

Commit ba68fb5

Browse files
committed
feat: Move specs from SDK
1 parent 848c101 commit ba68fb5

35 files changed

+2175
-0
lines changed

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ module github.com/cloudquery/plugin-pb-go
33
go 1.19
44

55
require (
6+
github.com/ghodss/yaml v1.0.0
7+
github.com/google/go-cmp v0.5.9
8+
github.com/stretchr/testify v1.4.0
9+
github.com/thoas/go-funk v0.9.3
610
google.golang.org/grpc v1.54.0
711
google.golang.org/protobuf v1.28.1
12+
gopkg.in/yaml.v3 v3.0.1
813
)
914

1015
require (
16+
github.com/davecgh/go-spew v1.1.0 // indirect
1117
github.com/golang/protobuf v1.5.2 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
1219
golang.org/x/net v0.8.0 // indirect
1320
golang.org/x/sys v0.6.0 // indirect
1421
golang.org/x/text v0.8.0 // indirect
1522
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
23+
gopkg.in/yaml.v2 v2.4.0 // indirect
1624
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
4+
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
15
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
26
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
37
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
48
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
59
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
10+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
14+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
15+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
16+
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
17+
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
618
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
719
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
820
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
@@ -18,3 +30,10 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
1830
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
1931
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
2032
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
33+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
34+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
35+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
36+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
37+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
38+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
39+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

specs/backend.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package specs
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
type Backend int
10+
11+
const (
12+
BackendNone Backend = iota
13+
BackendLocal
14+
)
15+
16+
var AllBackends = Backends{BackendNone, BackendLocal}
17+
var AllBackendNames = [...]string{
18+
BackendNone: "none",
19+
BackendLocal: "local",
20+
}
21+
22+
type Backends []Backend
23+
24+
func (s Backends) String() string {
25+
var buffer bytes.Buffer
26+
for i, backend := range s {
27+
if i > 0 {
28+
buffer.WriteString(", ")
29+
}
30+
buffer.WriteString(backend.String())
31+
}
32+
return buffer.String()
33+
}
34+
35+
func (s Backend) String() string {
36+
return AllBackendNames[s]
37+
}
38+
func (s Backend) MarshalJSON() ([]byte, error) {
39+
return []byte(`"` + s.String() + `"`), nil
40+
}
41+
42+
func (s *Backend) UnmarshalJSON(data []byte) (err error) {
43+
var backend string
44+
if err := json.Unmarshal(data, &backend); err != nil {
45+
return err
46+
}
47+
if *s, err = BackendFromString(backend); err != nil {
48+
return err
49+
}
50+
return nil
51+
}
52+
53+
func BackendFromString(s string) (Backend, error) {
54+
for i, backend := range AllBackendNames {
55+
if s == backend {
56+
return Backend(i), nil
57+
}
58+
}
59+
return BackendNone, fmt.Errorf("unknown backend %s", s)
60+
}

specs/backend_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package specs
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"gopkg.in/yaml.v3"
8+
)
9+
10+
func TestBackendJsonMarshalUnmarshal(t *testing.T) {
11+
b, err := json.Marshal(BackendLocal)
12+
if err != nil {
13+
t.Fatal("failed to marshal backend:", err)
14+
}
15+
var backend Backend
16+
if err := json.Unmarshal(b, &backend); err != nil {
17+
t.Fatal("failed to unmarshal backend:", err)
18+
}
19+
if backend != BackendLocal {
20+
t.Fatal("expected backend to be local, but got:", backend)
21+
}
22+
}
23+
24+
func TestBackendYamlMarshalUnmarshal(t *testing.T) {
25+
b, err := yaml.Marshal(BackendLocal)
26+
if err != nil {
27+
t.Fatal("failed to marshal backend:", err)
28+
}
29+
var backend Backend
30+
if err := yaml.Unmarshal(b, &backend); err != nil {
31+
t.Fatal("failed to unmarshal backend:", err)
32+
}
33+
if backend != BackendLocal {
34+
t.Fatal("expected backend to be local, but got:", backend)
35+
}
36+
}

specs/destination.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package specs
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"strings"
8+
9+
"github.com/thoas/go-funk"
10+
)
11+
12+
type Destination struct {
13+
Name string `json:"name,omitempty"`
14+
Version string `json:"version,omitempty"`
15+
Path string `json:"path,omitempty"`
16+
Registry Registry `json:"registry,omitempty"`
17+
WriteMode WriteMode `json:"write_mode,omitempty"`
18+
MigrateMode MigrateMode `json:"migrate_mode,omitempty"`
19+
BatchSize int `json:"batch_size,omitempty"`
20+
BatchSizeBytes int `json:"batch_size_bytes,omitempty"`
21+
Spec any `json:"spec,omitempty"`
22+
PKMode PKMode `json:"pk_mode,omitempty"`
23+
}
24+
25+
func (d *Destination) SetDefaults(defaultBatchSize, defaultBatchSizeBytes int) {
26+
if d.Registry.String() == "" {
27+
d.Registry = RegistryGithub
28+
}
29+
if d.BatchSize == 0 {
30+
d.BatchSize = defaultBatchSize
31+
}
32+
if d.BatchSizeBytes == 0 {
33+
d.BatchSizeBytes = defaultBatchSizeBytes
34+
}
35+
}
36+
37+
func (d *Destination) UnmarshalSpec(out any) error {
38+
b, err := json.Marshal(d.Spec)
39+
if err != nil {
40+
return err
41+
}
42+
dec := json.NewDecoder(bytes.NewReader(b))
43+
dec.UseNumber()
44+
dec.DisallowUnknownFields()
45+
return dec.Decode(out)
46+
}
47+
48+
func (d *Destination) Validate() error {
49+
if d.Name == "" {
50+
return fmt.Errorf("name is required")
51+
}
52+
if d.Path == "" {
53+
msg := "path is required"
54+
// give a small hint to help users transition from the old config format that didn't require path
55+
officialPlugins := []string{"postgresql", "csv"}
56+
if funk.ContainsString(officialPlugins, d.Name) {
57+
msg += fmt.Sprintf(". Hint: try setting path to cloudquery/%s in your config", d.Name)
58+
}
59+
return fmt.Errorf(msg)
60+
}
61+
62+
if d.Registry == RegistryGithub {
63+
if d.Version == "" {
64+
return fmt.Errorf("version is required")
65+
}
66+
if !strings.HasPrefix(d.Version, "v") {
67+
return fmt.Errorf("version must start with v")
68+
}
69+
}
70+
if d.BatchSize < 0 {
71+
return fmt.Errorf("batch_size must be greater than 0")
72+
}
73+
return nil
74+
}
75+
76+
func (d Destination) VersionString() string {
77+
if d.Registry != RegistryGithub {
78+
return fmt.Sprintf("%s (%s@%s)", d.Name, d.Registry, d.Path)
79+
}
80+
pathParts := strings.Split(d.Path, "/")
81+
if len(pathParts) != 2 {
82+
return fmt.Sprintf("%s (%s@%s)", d.Name, d.Path, d.Version)
83+
}
84+
if d.Name == pathParts[1] {
85+
return fmt.Sprintf("%s (%s)", d.Name, d.Version)
86+
}
87+
return fmt.Sprintf("%s (%s@%s)", d.Name, pathParts[1], d.Version)
88+
}

0 commit comments

Comments
 (0)