pulumi/sdk/go/common/util/mapper/mapper_encode.go

168 lines
4.8 KiB
Go
Raw Normal View History

2018-05-22 19:43:36 +00:00
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mapper
import (
"reflect"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)
// Encode encodes a strongly typed struct into a weakly typed JSON-like property bag.
func (md *mapper) Encode(source interface{}) (map[string]interface{}, MappingError) {
if source == nil {
return nil, nil
}
return md.encode(reflect.ValueOf(source))
}
func (md *mapper) encode(vsrc reflect.Value) (map[string]interface{}, MappingError) {
contract.Requiref(vsrc.IsValid(), "vsrc", "value must be valid")
// Fetch the type; if it's a pointer, do a quick nil check, otherwise operate on its underlying type.
vsrcType := vsrc.Type()
if vsrcType.Kind() == reflect.Ptr {
if vsrc.IsNil() {
return nil, nil
}
vsrc = vsrc.Elem()
vsrcType = vsrc.Type()
}
contract.Assertf(vsrcType.Kind() == reflect.Struct,
"Source %v must be a struct type with `pulumi:\"x\"` tags to direct encoding (kind %v)",
vsrcType, vsrcType.Kind())
// Fetch the source type, allocate a fresh object, and start encoding into it.
var errs []error
obj := make(map[string]interface{})
for _, fldtag := range md.structFieldsTags(vsrc.Type()) {
if !fldtag.Skip {
key := fldtag.Key
fld := vsrc.FieldByName(fldtag.Info.Name)
v, err := md.encodeValue(fld)
if err != nil {
errs = append(errs, err.Failures()...)
} else if v == nil {
if !fldtag.Optional && !md.opts.IgnoreMissing {
// The field doesn't exist and yet it is required; issue an error.
errs = append(errs, NewMissingError(vsrcType, key))
}
} else {
obj[key] = v
}
}
}
// If there are no errors, return nil; else manufacture a decode error object.
var err MappingError
if len(errs) > 0 {
err = NewMappingError(errs)
}
return obj, err
}
// EncodeValue decodes primitive type fields. For fields of complex types, we use custom deserialization.
func (md *mapper) EncodeValue(v interface{}) (interface{}, MappingError) {
if v == nil {
return nil, nil
}
return md.encodeValue(reflect.ValueOf(v))
}
func (md *mapper) encodeValue(vsrc reflect.Value) (interface{}, MappingError) {
contract.Requiref(vsrc.IsValid(), "vsrc", "value must be valid")
// Otherwise, try to map to the closest JSON-like destination type we can.
switch k := vsrc.Kind(); k {
// Primitive types:
case reflect.Bool:
return vsrc.Bool(), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(vsrc.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return float64(vsrc.Uint()), nil
case reflect.Float32, reflect.Float64:
return vsrc.Float(), nil
case reflect.String:
return vsrc.String(), nil
// Pointers:
case reflect.Ptr:
if vsrc.IsNil() {
return nil, nil
}
return md.encodeValue(vsrc.Elem())
// Slices and maps:
case reflect.Slice:
if vsrc.IsNil() {
return nil, nil
}
slice := make([]interface{}, vsrc.Len())
var errs []error
for i := 0; i < vsrc.Len(); i++ {
ev := vsrc.Index(i)
if elem, err := md.encodeValue(ev); err != nil {
errs = append(errs, err.Failures()...)
} else {
slice[i] = elem
}
}
if errs == nil {
return slice, nil
}
return nil, NewMappingError(errs)
case reflect.Map:
if vsrc.IsNil() {
return nil, nil
}
ktype := vsrc.Type().Key()
contract.Assertf(ktype.Kind() == reflect.String, "expected map with string keys, got %v (%v)", ktype, ktype.Kind())
iter := vsrc.MapRange()
mmap := make(map[string]interface{}, vsrc.Len())
var errs []error
for iter.Next() {
if val, err := md.encodeValue(iter.Value()); err != nil {
errs = append(errs, err.Failures()...)
} else {
mmap[iter.Key().String()] = val
}
}
if errs == nil {
return mmap, nil
}
return nil, NewMappingError(errs)
// Structs and interface{}:
case reflect.Struct:
return md.encode(vsrc)
case reflect.Interface:
Fix panic in mapper_encode (#14498) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description Fixes a panic that occurs when attempting to encode certain values, for example a map that contains a nil value: ```go bag := bagtag{ String: "something", StringOpt: "ohmv", MapOpt: map[string]interface{}{ "a": "something", "b": nil, // this reflect.Value is of type Interface and IsNil is true. }, } _ = resource.NewPropertyMap(bag) // panic here ``` New tests are provided to cover the above scenario and to cover each type of value, including nil cases where appropriate. Fixes: https://github.com/pulumi/pulumi-kubernetes/issues/2622 Follow-up to: https://github.com/pulumi/pulumi/pull/9810 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-11-04 18:31:22 +00:00
if vsrc.IsNil() {
return nil, nil
}
return md.encodeValue(vsrc.Elem())
turn on the golangci-lint exhaustive linter (#15028) Turn on the golangci-lint exhaustive linter. This is the first step towards catching more missing cases during development rather than in tests, or in production. This might be best reviewed commit-by-commit, as the first commit turns on the linter with the `default-signifies-exhaustive: true` option set, which requires a lot less changes in the current codebase. I think it's probably worth doing the second commit as well, as that will get us the real benefits, even though we end up with a little bit more churn. However it means all the `switch` statements are covered, which isn't the case after the first commit, since we do have a lot of `default` statements that just call `assert.Fail`. Fixes #14601 ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-01-17 16:50:41 +00:00
// Cases we don't handle
case reflect.Invalid, reflect.Complex64, reflect.Complex128, reflect.Array,
reflect.Chan, reflect.Func, reflect.UnsafePointer:
contract.Failf("Unrecognized field type '%v' during encoding", k)
default:
contract.Failf("Unrecognized field type '%v' during encoding", k)
}
return nil, nil
}