-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
848c101
commit ba68fb5
Showing
35 changed files
with
2,175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package specs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Backend int | ||
|
||
const ( | ||
BackendNone Backend = iota | ||
BackendLocal | ||
) | ||
|
||
var AllBackends = Backends{BackendNone, BackendLocal} | ||
var AllBackendNames = [...]string{ | ||
BackendNone: "none", | ||
BackendLocal: "local", | ||
} | ||
|
||
type Backends []Backend | ||
|
||
func (s Backends) String() string { | ||
var buffer bytes.Buffer | ||
for i, backend := range s { | ||
if i > 0 { | ||
buffer.WriteString(", ") | ||
} | ||
buffer.WriteString(backend.String()) | ||
} | ||
return buffer.String() | ||
} | ||
|
||
func (s Backend) String() string { | ||
return AllBackendNames[s] | ||
} | ||
func (s Backend) MarshalJSON() ([]byte, error) { | ||
return []byte(`"` + s.String() + `"`), nil | ||
} | ||
|
||
func (s *Backend) UnmarshalJSON(data []byte) (err error) { | ||
var backend string | ||
if err := json.Unmarshal(data, &backend); err != nil { | ||
return err | ||
} | ||
if *s, err = BackendFromString(backend); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func BackendFromString(s string) (Backend, error) { | ||
for i, backend := range AllBackendNames { | ||
if s == backend { | ||
return Backend(i), nil | ||
} | ||
} | ||
return BackendNone, fmt.Errorf("unknown backend %s", s) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package specs | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestBackendJsonMarshalUnmarshal(t *testing.T) { | ||
b, err := json.Marshal(BackendLocal) | ||
if err != nil { | ||
t.Fatal("failed to marshal backend:", err) | ||
} | ||
var backend Backend | ||
if err := json.Unmarshal(b, &backend); err != nil { | ||
t.Fatal("failed to unmarshal backend:", err) | ||
} | ||
if backend != BackendLocal { | ||
t.Fatal("expected backend to be local, but got:", backend) | ||
} | ||
} | ||
|
||
func TestBackendYamlMarshalUnmarshal(t *testing.T) { | ||
b, err := yaml.Marshal(BackendLocal) | ||
if err != nil { | ||
t.Fatal("failed to marshal backend:", err) | ||
} | ||
var backend Backend | ||
if err := yaml.Unmarshal(b, &backend); err != nil { | ||
t.Fatal("failed to unmarshal backend:", err) | ||
} | ||
if backend != BackendLocal { | ||
t.Fatal("expected backend to be local, but got:", backend) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package specs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/thoas/go-funk" | ||
) | ||
|
||
type Destination struct { | ||
Name string `json:"name,omitempty"` | ||
Version string `json:"version,omitempty"` | ||
Path string `json:"path,omitempty"` | ||
Registry Registry `json:"registry,omitempty"` | ||
WriteMode WriteMode `json:"write_mode,omitempty"` | ||
MigrateMode MigrateMode `json:"migrate_mode,omitempty"` | ||
BatchSize int `json:"batch_size,omitempty"` | ||
BatchSizeBytes int `json:"batch_size_bytes,omitempty"` | ||
Spec any `json:"spec,omitempty"` | ||
PKMode PKMode `json:"pk_mode,omitempty"` | ||
} | ||
|
||
func (d *Destination) SetDefaults(defaultBatchSize, defaultBatchSizeBytes int) { | ||
if d.Registry.String() == "" { | ||
d.Registry = RegistryGithub | ||
} | ||
if d.BatchSize == 0 { | ||
d.BatchSize = defaultBatchSize | ||
} | ||
if d.BatchSizeBytes == 0 { | ||
d.BatchSizeBytes = defaultBatchSizeBytes | ||
} | ||
} | ||
|
||
func (d *Destination) UnmarshalSpec(out any) error { | ||
b, err := json.Marshal(d.Spec) | ||
if err != nil { | ||
return err | ||
} | ||
dec := json.NewDecoder(bytes.NewReader(b)) | ||
dec.UseNumber() | ||
dec.DisallowUnknownFields() | ||
return dec.Decode(out) | ||
} | ||
|
||
func (d *Destination) Validate() error { | ||
if d.Name == "" { | ||
return fmt.Errorf("name is required") | ||
} | ||
if d.Path == "" { | ||
msg := "path is required" | ||
// give a small hint to help users transition from the old config format that didn't require path | ||
officialPlugins := []string{"postgresql", "csv"} | ||
if funk.ContainsString(officialPlugins, d.Name) { | ||
msg += fmt.Sprintf(". Hint: try setting path to cloudquery/%s in your config", d.Name) | ||
} | ||
return fmt.Errorf(msg) | ||
} | ||
|
||
if d.Registry == RegistryGithub { | ||
if d.Version == "" { | ||
return fmt.Errorf("version is required") | ||
} | ||
if !strings.HasPrefix(d.Version, "v") { | ||
return fmt.Errorf("version must start with v") | ||
} | ||
} | ||
if d.BatchSize < 0 { | ||
return fmt.Errorf("batch_size must be greater than 0") | ||
} | ||
return nil | ||
} | ||
|
||
func (d Destination) VersionString() string { | ||
if d.Registry != RegistryGithub { | ||
return fmt.Sprintf("%s (%s@%s)", d.Name, d.Registry, d.Path) | ||
} | ||
pathParts := strings.Split(d.Path, "/") | ||
if len(pathParts) != 2 { | ||
return fmt.Sprintf("%s (%s@%s)", d.Name, d.Path, d.Version) | ||
} | ||
if d.Name == pathParts[1] { | ||
return fmt.Sprintf("%s (%s)", d.Name, d.Version) | ||
} | ||
return fmt.Sprintf("%s (%s@%s)", d.Name, pathParts[1], d.Version) | ||
} |
Oops, something went wrong.