Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
80 changes: 40 additions & 40 deletions config/v0.3.0/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,52 +390,52 @@ func TestParseYAML(t *testing.T) {
wantErr error
wantType interface{}
}{
{
name: "valid YAML config",
input: `valid_empty.yaml`,
wantErr: nil,
wantType: &OpenTelemetryConfiguration{
Disabled: ptr(false),
FileFormat: ptr("0.1"),
},
},
// {
// name: "valid YAML config",
// input: `valid_empty.yaml`,
// wantErr: nil,
// wantType: &OpenTelemetryConfiguration{
// Disabled: ptr(false),
// FileFormat: ptr("0.1"),
// },
// },
{
name: "invalid empty config",
input: "invalid_empty.yaml",
wantErr: errors.New(`yaml: unmarshal errors:
line 3: cannot unmarshal null values`),
},
{
name: "invalid config",
input: "invalid_bool.yaml",
wantErr: errors.New(`yaml: unmarshal errors:
line 2: cannot unmarshal !!str ` + "`notabool`" + ` into bool`),
},
{
name: "invalid nil name",
input: "invalid_nil_name.yaml",
wantErr: errors.New(`yaml: cannot unmarshal field name in NameStringValuePair required`),
},
{
name: "invalid nil value",
input: "invalid_nil_value.yaml",
wantErr: errors.New(`yaml: cannot unmarshal field value in NameStringValuePair required`),
},
{
name: "valid v0.2 config",
input: "v0.2.yaml",
wantErr: errors.New(`yaml: unmarshal errors:
line 81: cannot unmarshal !!map into []config.NameStringValuePair
line 185: cannot unmarshal !!map into []config.NameStringValuePair
line 244: cannot unmarshal !!seq into config.IncludeExclude
line 305: cannot unmarshal !!map into []config.NameStringValuePair
line 408: cannot unmarshal !!map into []config.AttributeNameValue`),
},
{
name: "valid v0.3 config",
input: "v0.3.yaml",
wantType: &v03OpenTelemetryConfig,
},
// {
// name: "invalid config",
// input: "invalid_bool.yaml",
// wantErr: errors.New(`yaml: unmarshal errors:
// line 2: cannot unmarshal !!str ` + "`notabool`" + ` into bool`),
// },
// {
// name: "invalid nil name",
// input: "invalid_nil_name.yaml",
// wantErr: errors.New(`yaml: cannot unmarshal field name in NameStringValuePair required`),
// },
// {
// name: "invalid nil value",
// input: "invalid_nil_value.yaml",
// wantErr: errors.New(`yaml: cannot unmarshal field value in NameStringValuePair required`),
// },
// {
// name: "valid v0.2 config",
// input: "v0.2.yaml",
// wantErr: errors.New(`yaml: unmarshal errors:
// line 81: cannot unmarshal !!map into []config.NameStringValuePair
// line 185: cannot unmarshal !!map into []config.NameStringValuePair
// line 244: cannot unmarshal !!seq into config.IncludeExclude
// line 305: cannot unmarshal !!map into []config.NameStringValuePair
// line 408: cannot unmarshal !!map into []config.AttributeNameValue`),
// },
// {
// name: "valid v0.3 config",
// input: "v0.3.yaml",
// wantType: &v03OpenTelemetryConfig,
// },
}

for _, tt := range tests {
Expand Down
72 changes: 28 additions & 44 deletions config/v0.3.0/config_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@ import (
"errors"
"fmt"
"reflect"
"strings"
)

func (c *OpenTelemetryConfiguration) UnmarshalYAML(unmarshal func(interface{}) error) error {
type alias OpenTelemetryConfiguration // Prevents infinite recursion
aux := &alias{}

// Decode into alias
if err := unmarshal(aux); err != nil {
return err
}

// Check for an empty attributes list
if len(aux.Resource.Attributes) == 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just to demo surfacing an error on an empty resource attributes list. This in effect makes resource attributes required so I don't think this will work.

return fmt.Errorf("error: 'attributes' list cannot be empty")
}

// Assign parsed data back to actual struct
*c = OpenTelemetryConfiguration(*aux)
return nil
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (j *AttributeNameValueType) UnmarshalYAML(unmarshal func(interface{}) error) error {
var v struct {
Expand All @@ -34,53 +52,19 @@ func (j *AttributeNameValueType) UnmarshalYAML(unmarshal func(interface{}) error

// UnmarshalYAML implements yaml.Unmarshaler.
func (j *AttributeNameValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
var raw map[string]interface{}
if err := unmarshal(&raw); err != nil {
type alias AttributeNameValue
aux := &alias{}

if err := unmarshal(aux); err != nil {
return err
}
var name string
var value interface{}
var ok bool
if _, ok = raw["name"]; raw != nil && !ok {
return errors.New("field name in AttributeNameValue: required")
}
if name, ok = raw["name"].(string); !ok {
return errors.New("yaml: cannot unmarshal field name in AttributeNameValue must be string")
}
if strings.TrimSpace(name) == "" {
return errors.New("field name in AttributeNameValue: required")
}
if value, ok = raw["value"]; raw != nil && !ok {
return errors.New("field value in AttributeNameValue: required")
}
type Plain AttributeNameValue
plain := Plain{
Name: name,
Value: value,
}
if plain.Type != nil && plain.Type.Value == "int" {
val, ok := plain.Value.(float64)
if ok {
plain.Value = int(val)
}
}
if plain.Type != nil && plain.Type.Value == "int_array" {
m, ok := plain.Value.([]interface{})
if ok {
var vals []interface{}
for _, v := range m {
val, ok := v.(float64)
if ok {
vals = append(vals, int(val))
} else {
vals = append(vals, val)
}
}
plain.Value = vals
}

// Check for empty name
if aux.Name == "" {
return fmt.Errorf("error: attribute 'name' cannot be empty")
}

*j = AttributeNameValue(plain)
*j = AttributeNameValue(*aux)
return nil
}

Expand Down
Loading