apple / pkl

A configuration as code language with rich validation and tooling.
https://pkl-lang.org
Apache License 2.0
10.36k stars 280 forks source link

Custom ResourceReader implementation in Golang fails to decode read data #656

Open barahona42 opened 1 month ago

barahona42 commented 1 month ago

context

Hi all, first time submitting a bug report ever. Here's the info:

outputs

Any pointers/insight on this would be appreciated. Thank you.

HT154 commented 1 month ago

The issue with returning an empty/nil byte slice is definitely a bug! The read contents are assumed to be non-null even though an empty byte slice on the go side is encoded as nil. A fix similar to https://github.com/apple/pkl/pull/480 is needed for this, which should be pretty straightforward. I'll probably have a PR up for this later today or tomorrow.

HT154 commented 1 month ago

The other issue is not as clear. Can you share some of the exact code that produced that error?

You definitely don't need to do the msgpack encoding yourself, just return the string produced by your resource directly, eg. return []byte("foo"), nil

PS: great bug report! ❤️

barahona42 commented 1 month ago

Thanks for the response. Here's the implementation of the resource and the code that gets the ssm parameter value.

// wraps ssm call
func GetDecryptedParameter(ctx context.Context, name string) (*string, error) {
    // do some service initialization
    input := ssm.GetParameterInput{Name: aws.String(name), WithDecryption: aws.Bool(true)} 
        // call to aws sdk method
    output, err := svc.GetParameter(ctx, &input)
    if err != nil {
        return nil, err
    }
    return output.Parameter.Value, nil
} 

// pkl.ResourceReader implementation
type PklResourceReader struct{}

// Scheme: pkl.Reader implementation
func (r PklResourceReader) Scheme() string {
    return "ssm"
}

// IsGlobbable: pkl.Reader implementation
func (r PklResourceReader) IsGlobbable() bool {
    return false
}

// HasHierarchicalUris: pkl.Reader implementation
func (r PklResourceReader) HasHierarchicalUris() bool {
    return false
}

// ListElements: pkl.Reader implementation
func (r PklResourceReader) ListElements(url url.URL) ([]pkl.PathElement, error) {
    return nil, nil
}

// Read: pkl.Reader implementation
func (r PklResourceReader) Read(url url.URL) ([]byte, error) {
    v, err := GetDecryptedParameter(context.Background(), url.Path)
    if err != nil {
        return nil, err
    }
    if v == nil {
        return make([]byte, 0), nil
    }
    return []byte(*v), nil
}