Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding paramgen #44

Merged
merged 21 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .golangci.goheader.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright © {{ YEAR }} Meroxa, Inc.
Copyright © {{ copyright-year }} Meroxa, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ linters-settings:
ignore-tests: true
goheader:
template-path: '.golangci.goheader.template'
values:
regexp:
copyright-year: 20[2-9]\d
gosec:
config:
# Maximum allowed permissions mode for os.WriteFile and ioutil.WriteFile
# Default is 0600, we use 0644 since it's macOS default, and we are not
# dealing with critical data.
G306: "0644"

issues:
exclude-rules:
Expand Down
136 changes: 136 additions & 0 deletions cmd/paramgen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#ParamGen

ParamGen is a conduit tool that generates the code to return the parameters map from a certain Go struct.

##Installation
Once you have installed Go, install the paramgen tool.
````
go install github.com/ConduitIO/conduit-connector-sdk/cmd/paramgen@v0.4.1
````

##Usage
ParamGen has one required argument, which is the struct name, and two optional flags for the path and the output file name.
````
paramgen [-path] [-output] structName
````
Example:
````
paramgen -path=./source -output=source_params SourceConfig
````
This example will search for a struct called `SourceConfig` in the path `./source`, it will create a parameter map of
only the exported fields, and generate the code to return this map in the file `source_params.go` under the same folder.

###Parameter Tags
In order to give your parameter a name, a default value, or add some validations to it, tags are the way to go.
We have three tags that can be parsed:
1. `json`: this tag is used to rename the parameter.
```go
Name string `json:"first-name"`

2. `default`: sets the default value for the parameter.
```go
Name string `default:"conduit"`
3. `validate`: adds builtin validations to the parameter, these validations will be executed by conduit once a connector
is configured. Validations are separated by a comma, and have 6 main types:
* `required`: a boolean tag to indicate if a field is required or not. If it is added to the validate tag without a
value, then we assume the field is required.
```go
NameRequired string `validate:"required"`
NameRequired2 string `validate:"required=true"`
NameNotRequired string `validate:"required=false"`
* `lt` or `less-than`: takes an int or a float value, indicated that the parameter should be less than the value provided.
* `gt` or `greater-than`: takes an int or a float value, indicated that the parameter should be greater than the value provided.

```go
Age int `validate:"gt=0,lt=200"`
Age2 float `validate:"greater-than=0,less-than=200.2"`
* `inclusion`: validates that the parameter value is included in a specified list, this list values are separated
using a pipe character `|`.
```go
Gender string `validate:"inclusion=male|female|other"`
* `exclusion`: validates that the parameter value is NOT included in a specified list, this list values are separated
using a pipe character `|`.
```go
Color string `validate:"exclusion=red|green"`
* `regex`: validates that the parameter value matches the regex pattern.
```go
Email string `validate:"regex=^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"`

##Example
Assume we have this configuration struct:
````go
package source
type SourceConfig struct {
InnerConfig
// Param1 my param1 description
Param1 int `validate:"required, gt=0, lt=100" default:"10"`
// comment about param2
Param2 bool `validate:"inclusion=true|t, exclusion=false|f" default:"t"`
Param3 string `validate:"regex=.*" default:"yes"`

// this will be ignored because it's not exported
privateParam string
}

type InnerConfig struct {
Name string `validate:"required" json:"my-name"`
}
````
And you call ParamGen:
````
paramgen -path ./source SourceConfig
````
A file called `paramgen.go` will be created under `./source`:
````go
// Code generated by ParamGen. DO NOT EDIT.
// Source: github.com/conduitio/conduit-connector-sdk/cmd/paramgen

package source

import (
"regexp"

sdk "github.com/conduitio/conduit-connector-sdk"
)

func (SourceConfig) Parameters() map[string]sdk.Parameter {
return map[string]sdk.Parameter{
"innerConfig.my-name": {
Default: "",
Description: "",
Type: sdk.ParameterTypeString,
Validations: []sdk.Validation{
sdk.ValidationRequired{},
},
},
"param1": {
Default: "10",
Description: "param1 my param1 description",
Type: sdk.ParameterTypeInt,
Validations: []sdk.Validation{
sdk.ValidationRequired{},
sdk.ValidationGreaterThan{Value: 0},
sdk.ValidationLessThan{Value: 100},
},
},
"param2": {
Default: "t",
Description: "comment about param2",
Type: sdk.ParameterTypeBool,
Validations: []sdk.Validation{
sdk.ValidationInclusion{List: []string{"true", "t"}},
sdk.ValidationExclusion{List: []string{"false", "f"}},
},
},
"param3": {
Default: "yes",
Description: "",
Type: sdk.ParameterTypeString,
Validations: []sdk.Validation{
sdk.ValidationRegex{Regex: regexp.MustCompile(".*")},
},
},
}
}

````
Loading