...

Source file src/github.com/redhat-developer/odo/pkg/service/crd_builder.go

Documentation: github.com/redhat-developer/odo/pkg/service

     1  package service
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/go-openapi/spec"
     9  )
    10  
    11  // BuildCRDFromParams iterates over the parameter maps provided by the user and builds the CRD
    12  func BuildCRDFromParams(paramMap map[string]string, crd *spec.Schema, group, version, kind string) (map[string]interface{}, error) {
    13  	spec := map[string]interface{}{}
    14  	for k, v := range paramMap {
    15  		err := addParam(spec, crd, k, v)
    16  		if err != nil {
    17  			return nil, err
    18  		}
    19  	}
    20  
    21  	result := map[string]interface{}{}
    22  	result["apiVersion"] = group + "/" + version
    23  	result["kind"] = kind
    24  	result["metadata"] = make(map[string]interface{})
    25  	result["spec"] = spec
    26  	return result, nil
    27  }
    28  
    29  func addParam(m map[string]interface{}, crd *spec.Schema, key string, value string) error {
    30  	if strings.Contains(key, ".") {
    31  		parts := strings.SplitN(key, ".", 2)
    32  		property := parts[0]
    33  		_, found := m[property]
    34  		if !found {
    35  			m[property] = map[string]interface{}{}
    36  		}
    37  		submap, ok := m[property].(map[string]interface{})
    38  		if !ok {
    39  			return errors.New("already defined")
    40  		}
    41  		var subCRD *spec.Schema
    42  		if crd != nil {
    43  			s := crd.Properties[property]
    44  			subCRD = &s
    45  		}
    46  		err := addParam(submap, subCRD, parts[1], value)
    47  		if err != nil {
    48  			return err
    49  		}
    50  	} else {
    51  		if _, found := m[key]; found {
    52  			return errors.New("already defined")
    53  		}
    54  
    55  		var subCRD *spec.Schema
    56  		if crd != nil {
    57  			s := crd.Properties[key]
    58  			subCRD = &s
    59  		}
    60  		m[key] = convertType(subCRD, value)
    61  	}
    62  	return nil
    63  }
    64  
    65  func convertType(crd *spec.Schema, value string) interface{} {
    66  	if crd != nil {
    67  		// do not use 'else' as the Schema can accept several types
    68  		// the first matching type will be used
    69  		if crd.Type.Contains("string") {
    70  			return value
    71  		}
    72  		if crd.Type.Contains("integer") {
    73  			intv, err := strconv.ParseInt(value, 10, 64)
    74  			if err == nil {
    75  				return intv
    76  			}
    77  		}
    78  		if crd.Type.Contains("number") {
    79  			floatv, err := strconv.ParseFloat(value, 64)
    80  			if err == nil {
    81  				return floatv
    82  			}
    83  		}
    84  		if crd.Type.Contains("boolean") {
    85  			boolv, err := strconv.ParseBool(value)
    86  			if err == nil {
    87  				return boolv
    88  			}
    89  		}
    90  	} else {
    91  		// no crd information available, guess the type depending on the value
    92  		intv, err := strconv.ParseInt(value, 10, 64)
    93  		if err == nil {
    94  			return intv
    95  		}
    96  
    97  		floatv, err := strconv.ParseFloat(value, 64)
    98  		if err == nil {
    99  			return floatv
   100  		}
   101  
   102  		boolv, err := strconv.ParseBool(value)
   103  		if err == nil {
   104  			return boolv
   105  		}
   106  	}
   107  
   108  	// as a last resort return the string value
   109  	return value
   110  }
   111  

View as plain text