github.com/aavshr/aws-sdk-go@v1.41.3/private/model/api/param_filler.go (about)

     1  //go:build codegen
     2  // +build codegen
     3  
     4  package api
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"reflect"
    10  	"strings"
    11  
    12  	"github.com/aavshr/aws-sdk-go/private/util"
    13  )
    14  
    15  // A paramFiller provides string formatting for a shape and its types.
    16  type paramFiller struct {
    17  	prefixPackageName bool
    18  }
    19  
    20  // typeName returns the type name of a shape.
    21  func (f paramFiller) typeName(shape *Shape) string {
    22  	if f.prefixPackageName && shape.Type == "structure" {
    23  		return "*" + shape.API.PackageName() + "." + shape.GoTypeElem()
    24  	}
    25  	return shape.GoType()
    26  }
    27  
    28  // ParamsStructFromJSON returns a JSON string representation of a structure.
    29  func ParamsStructFromJSON(value interface{}, shape *Shape, prefixPackageName bool) string {
    30  	f := paramFiller{prefixPackageName: prefixPackageName}
    31  	return util.GoFmt(f.paramsStructAny(value, shape))
    32  }
    33  
    34  // paramsStructAny returns the string representation of any value.
    35  func (f paramFiller) paramsStructAny(value interface{}, shape *Shape) string {
    36  	if value == nil {
    37  		return ""
    38  	}
    39  
    40  	switch shape.Type {
    41  	case "structure":
    42  		if value != nil {
    43  			vmap := value.(map[string]interface{})
    44  			return f.paramsStructStruct(vmap, shape)
    45  		}
    46  	case "list":
    47  		vlist := value.([]interface{})
    48  		return f.paramsStructList(vlist, shape)
    49  	case "map":
    50  		vmap := value.(map[string]interface{})
    51  		return f.paramsStructMap(vmap, shape)
    52  	case "string", "character":
    53  		v := reflect.Indirect(reflect.ValueOf(value))
    54  		if v.IsValid() {
    55  			return fmt.Sprintf("aws.String(%#v)", v.Interface())
    56  		}
    57  	case "blob":
    58  		v := reflect.Indirect(reflect.ValueOf(value))
    59  		if v.IsValid() && shape.Streaming {
    60  			return fmt.Sprintf("bytes.NewReader([]byte(%#v))", v.Interface())
    61  		} else if v.IsValid() {
    62  			return fmt.Sprintf("[]byte(%#v)", v.Interface())
    63  		}
    64  	case "boolean":
    65  		v := reflect.Indirect(reflect.ValueOf(value))
    66  		if v.IsValid() {
    67  			return fmt.Sprintf("aws.Bool(%#v)", v.Interface())
    68  		}
    69  	case "integer", "long":
    70  		v := reflect.Indirect(reflect.ValueOf(value))
    71  		if v.IsValid() {
    72  			return fmt.Sprintf("aws.Int64(%v)", v.Interface())
    73  		}
    74  	case "float", "double":
    75  		v := reflect.Indirect(reflect.ValueOf(value))
    76  		if v.IsValid() {
    77  			return fmt.Sprintf("aws.Float64(%v)", v.Interface())
    78  		}
    79  	case "timestamp":
    80  		v := reflect.Indirect(reflect.ValueOf(value))
    81  		if v.IsValid() {
    82  			return fmt.Sprintf("aws.Time(time.Unix(%d, 0))", int(v.Float()))
    83  		}
    84  	case "jsonvalue":
    85  		v, err := json.Marshal(value)
    86  		if err != nil {
    87  			panic("failed to marshal JSONValue, " + err.Error())
    88  		}
    89  		const tmpl = `func() aws.JSONValue {
    90  			var m aws.JSONValue
    91  			if err := json.Unmarshal([]byte(%q), &m); err != nil {
    92  				panic("failed to unmarshal JSONValue, "+err.Error())
    93  			}
    94  			return m
    95  		}()`
    96  		return fmt.Sprintf(tmpl, string(v))
    97  	default:
    98  		panic("Unhandled type " + shape.Type)
    99  	}
   100  	return ""
   101  }
   102  
   103  // paramsStructStruct returns the string representation of a structure
   104  func (f paramFiller) paramsStructStruct(value map[string]interface{}, shape *Shape) string {
   105  	out := "&" + f.typeName(shape)[1:] + "{\n"
   106  	for _, n := range shape.MemberNames() {
   107  		ref := shape.MemberRefs[n]
   108  		name := findParamMember(value, n)
   109  
   110  		if val := f.paramsStructAny(value[name], ref.Shape); val != "" {
   111  			out += fmt.Sprintf("%s: %s,\n", n, val)
   112  		}
   113  	}
   114  	out += "}"
   115  	return out
   116  }
   117  
   118  // paramsStructMap returns the string representation of a map of values
   119  func (f paramFiller) paramsStructMap(value map[string]interface{}, shape *Shape) string {
   120  	out := f.typeName(shape) + "{\n"
   121  	keys := util.SortedKeys(value)
   122  	for _, k := range keys {
   123  		v := value[k]
   124  		out += fmt.Sprintf("%q: %s,\n", k, f.paramsStructAny(v, shape.ValueRef.Shape))
   125  	}
   126  	out += "}"
   127  	return out
   128  }
   129  
   130  // paramsStructList returns the string representation of slice of values
   131  func (f paramFiller) paramsStructList(value []interface{}, shape *Shape) string {
   132  	out := f.typeName(shape) + "{\n"
   133  	for _, v := range value {
   134  		out += fmt.Sprintf("%s,\n", f.paramsStructAny(v, shape.MemberRef.Shape))
   135  	}
   136  	out += "}"
   137  	return out
   138  }
   139  
   140  // findParamMember searches a map for a key ignoring case. Returns the map key if found.
   141  func findParamMember(value map[string]interface{}, key string) string {
   142  	for actualKey := range value {
   143  		if strings.EqualFold(key, actualKey) {
   144  			return actualKey
   145  		}
   146  	}
   147  	return ""
   148  }