github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/go/gen_program_utils.go (about)

     1  package gen
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  
     8  	"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
     9  )
    10  
    11  type promptToInputArrayHelper struct {
    12  	destType string
    13  }
    14  
    15  var primitives = map[string]string{
    16  	"String":  "string",
    17  	"Bool":    "bool",
    18  	"Int":     "int",
    19  	"Int64":   "int64",
    20  	"Float64": "float64",
    21  }
    22  
    23  func (p *promptToInputArrayHelper) generateHelperMethod(w io.Writer) {
    24  	promptType := p.getPromptItemType()
    25  	inputType := p.getInputItemType()
    26  	fnName := p.getFnName()
    27  	fmt.Fprintf(w, "func %s(arr []%s) %s {\n", fnName, promptType, p.destType)
    28  	fmt.Fprintf(w, "var pulumiArr %s\n", p.destType)
    29  	fmt.Fprintf(w, "for _, v := range arr {\n")
    30  	fmt.Fprintf(w, "pulumiArr = append(pulumiArr, %s(v))\n", inputType)
    31  	fmt.Fprintf(w, "}\n")
    32  	fmt.Fprintf(w, "return pulumiArr\n")
    33  	fmt.Fprintf(w, "}\n")
    34  }
    35  
    36  func (p *promptToInputArrayHelper) getFnName() string {
    37  	parts := strings.Split(p.destType, ".")
    38  	contract.Assertf(len(parts) == 2, "promptToInputArrayHelper destType expected to have two parts.")
    39  	return fmt.Sprintf("to%s%s", Title(parts[0]), Title(parts[1]))
    40  }
    41  
    42  func (p *promptToInputArrayHelper) getPromptItemType() string {
    43  	inputType := p.getInputItemType()
    44  	parts := strings.Split(inputType, ".")
    45  	contract.Assertf(len(parts) == 2, "promptToInputArrayHelper destType expected to have two parts.")
    46  	typ := parts[1]
    47  	if t, ok := primitives[typ]; ok {
    48  		return t
    49  	}
    50  
    51  	return typ
    52  }
    53  
    54  func (p *promptToInputArrayHelper) getInputItemType() string {
    55  	return strings.TrimSuffix(p.destType, "Array")
    56  }
    57  
    58  // Provides code for a method which will be placed in the program preamble if deemed
    59  // necessary. Because many tasks in Go such as reading a file require extensive error
    60  // handling, it is much prettier to encapsulate that error handling boilerplate as its
    61  // own function in the preamble.
    62  func getHelperMethodIfNeeded(functionName string) (string, bool) {
    63  	switch functionName {
    64  	case "readFile":
    65  		return `func readFileOrPanic(path string) pulumi.StringPtrInput {
    66  				data, err := ioutil.ReadFile(path)
    67  				if err != nil {
    68  					panic(err.Error())
    69  				}
    70  				return pulumi.String(string(data))
    71  			}`, true
    72  	case "filebase64":
    73  		return `func filebase64OrPanic(path string) pulumi.StringPtrInput {
    74  					if fileData, err := ioutil.ReadFile(path); err == nil {
    75  						return pulumi.String(base64.StdEncoding.EncodeToString(fileData[:]))
    76  					} else {
    77  						panic(err.Error())
    78  					}
    79  				}`, true
    80  	case "filebase64sha256":
    81  		return `func filebase64sha256OrPanic(path string) pulumi.StringPtrInput {
    82  					if fileData, err := ioutil.ReadFile(path); err == nil {
    83  						hashedData := sha256.Sum256([]byte(fileData))
    84  						return pulumi.String(base64.StdEncoding.EncodeToString(hashedData[:]))
    85  					} else {
    86  						panic(err.Error())
    87  					}
    88  				}`, true
    89  	case "sha1":
    90  		return `func sha1Hash(input string) string {
    91  				hash := sha1.Sum([]byte(input))
    92  				return hex.EncodeToString(hash[:])
    93  			}`, true
    94  	default:
    95  		return "", false
    96  	}
    97  }