Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/go_modules/google.golang.org/grpc…
Browse files Browse the repository at this point in the history
…-1.69.4
  • Loading branch information
hariso authored Jan 17, 2025
2 parents 6021036 + a16db5e commit bc34539
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
29 changes: 20 additions & 9 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ func parseConfig(
return err
}

func YAMLSpecification(rawYaml string) func() Specification {
specs, err := ParseYAMLSpecification(context.Background(), rawYaml)
// YAMLSpecification parses a Specification from the given YAML file.
// The connector version found in the YAML file can be overridden with
// the `version` parameter.
func YAMLSpecification(rawYaml, version string) func() Specification {
specs, err := ParseYAMLSpecification(context.Background(), rawYaml, version)
if err != nil {
panic("failed to parse YAML specification: " + err.Error())
}
return func() Specification { return specs }
}

func ParseYAMLSpecification(ctx context.Context, rawYaml string) (Specification, error) {
func ParseYAMLSpecification(ctx context.Context, rawYaml, version string) (Specification, error) {
logger := Logger(ctx)

logger.Debug().Str("yaml", rawYaml).Msg("parsing YAML specification")
Expand All @@ -124,7 +127,7 @@ func ParseYAMLSpecification(ctx context.Context, rawYaml string) (Specification,
)
reader := strings.NewReader(rawYaml)

spec, warnings, err := parser.Parse(ctx, reader)
specs, warnings, err := parser.Parse(ctx, reader)
if err != nil {
return Specification{}, fmt.Errorf("failed to parse YAML specification: %w", err)
}
Expand All @@ -133,17 +136,25 @@ func ParseYAMLSpecification(ctx context.Context, rawYaml string) (Specification,
warnings.Log(ctx, slogLogger)
}

switch len(spec) {
var spec Specification
switch len(specs) {
case 0:
logger.Debug().Msg("no specification found in YAML")
return Specification{}, fmt.Errorf("no specification found in YAML")
case 1:
logger.Debug().Any("specification", spec[0]).Msg("specification successfully parsed")
return spec[0], nil
logger.Debug().Any("specification", specs[0]).Msg("specification successfully parsed")
spec = specs[0]
default:
logger.Warn().Any("specification", spec[0]).Msg("multiple specifications found in YAML, returning the first one")
return spec[0], nil
logger.Warn().Any("specification", specs[0]).Msg("multiple specifications found in YAML, returning the first one")
spec = specs[0]
}

versionTrimmed := strings.TrimSpace(version)
if versionTrimmed != "" {
spec.Version = versionTrimmed
}

return spec, nil
}

func must[T any](out T, err error) T {
Expand Down
84 changes: 84 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,87 @@ func TestParseConfig_ValidateCalled(t *testing.T) {
err := Util.ParseConfig(context.Background(), cfg, &target, params)
is.True(errors.Is(err, wantErr))
}

func TestYAMLSpecification(t *testing.T) {
is := is.New(t)

yaml := `
version: "1.0"
specification:
name: foo
summary: describe your connector
description: describe your connector in detail
version: v0.6.0
author: your name
source:
parameters:
- name: sunny
description: sunny describes how sunny is it outside
type: string
default: ""
validations:
- type: required
value: ""
destination:
parameters:
- name: rainy
description: rainy describes how rainy is it outside
type: string
default: ""
validations:
- type: required
value: ""`

spec := Specification{
Name: "foo",
Summary: "describe your connector",
Description: "describe your connector in detail",
Version: "v0.6.0",
Author: "your name",
SourceParams: config.Parameters{
"sunny": {
Default: "",
Description: "sunny describes how sunny is it outside",
Type: config.ParameterTypeString,
Validations: []config.Validation{
config.ValidationRequired{},
},
},
},
DestinationParams: config.Parameters{
"rainy": {
Default: "",
Description: "rainy describes how rainy is it outside",
Type: config.ParameterTypeString,
Validations: []config.Validation{
config.ValidationRequired{},
},
},
},
}

testCases := []struct {
name string
version string
}{
{
name: "no version override",
version: "",
},
{
name: "with version override",
version: "v1.3.5-abcdef",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := YAMLSpecification(yaml, tc.version)()
want := spec
if tc.version != "" {
want.Version = tc.version
}
is.Equal(want, got)
})
}
}

0 comments on commit bc34539

Please sign in to comment.