Skip to content
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
7 changes: 3 additions & 4 deletions private/bufpkg/bufconfig/buf_gen_yaml_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package bufconfig
import (
"context"
"encoding/json"
"fmt"
"io"

"buf.build/go/standard/xslices"
Expand Down Expand Up @@ -179,7 +178,7 @@ func readBufGenYAMLFile(
case FileVersionV1Beta1:
var externalGenYAMLFile externalBufGenYAMLFileV1Beta1
if err := getUnmarshalStrict(allowJSON)(data, &externalGenYAMLFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
generateConfig, err := newGenerateConfigFromExternalFileV1Beta1(externalGenYAMLFile)
if err != nil {
Expand All @@ -194,7 +193,7 @@ func readBufGenYAMLFile(
case FileVersionV1:
var externalGenYAMLFile externalBufGenYAMLFileV1
if err := getUnmarshalStrict(allowJSON)(data, &externalGenYAMLFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
generateConfig, err := newGenerateConfigFromExternalFileV1(externalGenYAMLFile)
if err != nil {
Expand All @@ -209,7 +208,7 @@ func readBufGenYAMLFile(
case FileVersionV2:
var externalGenYAMLFile externalBufGenYAMLFileV2
if err := getUnmarshalStrict(allowJSON)(data, &externalGenYAMLFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
generateConfig, err := newGenerateConfigFromExternalFileV2(externalGenYAMLFile)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions private/bufpkg/bufconfig/buf_lock_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func readBufLockFile(
case FileVersionV1Beta1, FileVersionV1:
var externalBufLockFile externalBufLockFileV1Beta1V1
if err := getUnmarshalStrict(allowJSON)(data, &externalBufLockFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
depModuleKeys := make([]bufmodule.ModuleKey, len(externalBufLockFile.Deps))
for i, dep := range externalBufLockFile.Deps {
Expand Down Expand Up @@ -406,7 +406,7 @@ func readBufLockFile(
case FileVersionV2:
var externalBufLockFile externalBufLockFileV2
if err := getUnmarshalStrict(allowJSON)(data, &externalBufLockFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
depModuleKeys := make([]bufmodule.ModuleKey, len(externalBufLockFile.Deps))
for i, dep := range externalBufLockFile.Deps {
Expand Down
2 changes: 1 addition & 1 deletion private/bufpkg/bufconfig/buf_work_yaml_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func readBufWorkYAMLFile(
}
var externalBufWorkYAMLFile externalBufWorkYAMLFileV1
if err := getUnmarshalStrict(allowJSON)(data, &externalBufWorkYAMLFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
return newBufWorkYAMLFile(fileVersion, objectData, externalBufWorkYAMLFile.Directories)
}
Expand Down
4 changes: 2 additions & 2 deletions private/bufpkg/bufconfig/buf_yaml_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func readBufYAMLFile(
case FileVersionV1Beta1, FileVersionV1:
var externalBufYAMLFile externalBufYAMLFileV1Beta1V1
if err := getUnmarshalStrict(allowJSON)(data, &externalBufYAMLFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
if fileVersion == FileVersionV1 && len(externalBufYAMLFile.Build.Roots) > 0 {
return nil, fmt.Errorf("build.roots cannot be set on version %v: %v", fileVersion, externalBufYAMLFile.Build.Roots)
Expand Down Expand Up @@ -475,7 +475,7 @@ func readBufYAMLFile(
case FileVersionV2:
var externalBufYAMLFile externalBufYAMLFileV2
if err := getUnmarshalStrict(allowJSON)(data, &externalBufYAMLFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
externalModules := externalBufYAMLFile.Modules
if len(externalModules) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func readBufPolicyYAMLFile(
}
var externalBufPolicyYAMLFile externalBufPolicyYAMLFileV2
if err := getUnmarshalStrict(allowJSON)(data, &externalBufPolicyYAMLFile); err != nil {
return nil, fmt.Errorf("invalid as version %v: %w", fileVersion, err)
return nil, err
}
var lintConfig bufpolicy.LintConfig
if !externalBufPolicyYAMLFile.Lint.isEmpty() {
Expand Down
51 changes: 35 additions & 16 deletions private/pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ func UnmarshalJSONStrict(data []byte, v any) error {
}
jsonDecoder := json.NewDecoder(bytes.NewReader(data))
jsonDecoder.DisallowUnknownFields()
if err := jsonDecoder.Decode(v); err != nil {
return fmt.Errorf("could not unmarshal as JSON: %v", err)
}
return nil
return jsonDecoder.Decode(v)
}

// UnmarshalYAMLStrict unmarshals the data as YAML, returning a user error on failure.
Expand All @@ -49,10 +46,7 @@ func UnmarshalYAMLStrict(data []byte, v any) error {
return nil
}
yamlDecoder := NewYAMLDecoderStrict(bytes.NewReader(data))
if err := yamlDecoder.Decode(v); err != nil {
return fmt.Errorf("could not unmarshal as YAML: %v", err)
}
return nil
return updateYAMLTypeError(yamlDecoder.Decode(v))
}

// UnmarshalJSONOrYAMLStrict unmarshals the data as JSON or YAML in order, returning
Expand All @@ -79,10 +73,7 @@ func UnmarshalJSONNonStrict(data []byte, v any) error {
return nil
}
jsonDecoder := json.NewDecoder(bytes.NewReader(data))
if err := jsonDecoder.Decode(v); err != nil {
return fmt.Errorf("could not unmarshal as JSON: %v", err)
}
return nil
return jsonDecoder.Decode(v)
}

// UnmarshalYAMLNonStrict unmarshals the data as YAML, returning a user error on failure.
Expand All @@ -93,10 +84,7 @@ func UnmarshalYAMLNonStrict(data []byte, v any) error {
return nil
}
yamlDecoder := NewYAMLDecoderNonStrict(bytes.NewReader(data))
if err := yamlDecoder.Decode(v); err != nil {
return fmt.Errorf("could not unmarshal as YAML: %v", err)
}
return nil
return updateYAMLTypeError(yamlDecoder.Decode(v))
}

// UnmarshalJSONOrYAMLNonStrict unmarshals the data as JSON or YAML in order, returning
Expand Down Expand Up @@ -200,3 +188,34 @@ func InterfaceSliceOrStringToStringSlice(in any) ([]string, error) {
return nil, fmt.Errorf("could not interpret %T as string or string slice", in)
}
}

// *** PRIVATE ***

func updateYAMLTypeError(err error) error {
if err == nil {
return nil
}
var yamlTypeError *yaml.TypeError
if errors.As(err, &yamlTypeError) {
for i, errString := range yamlTypeError.Errors {
yamlTypeError.Errors[i] = replaceAfter(
replaceAfter(
errString,
"already set in type",
"already set",
),
"not found in type",
"not found",
)
}
return yamlTypeError
}
return err
}

func replaceAfter(s string, substitute string, replace string) string {
if index := strings.Index(s, substitute); index != -1 {
return s[:index] + replace
}
return s
}