github.com/SAP/cloud-mta-build-tool@v1.2.27/internal/platform/process.go (about)

     1  package platform
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	"gopkg.in/yaml.v2"
     6  
     7  	"github.com/SAP/cloud-mta/mta"
     8  )
     9  
    10  // Unmarshal - unmarshal platform config
    11  func Unmarshal(data []byte) (Platforms, error) {
    12  	platforms := Platforms{}
    13  	err := yaml.UnmarshalStrict(data, &platforms)
    14  	if err != nil {
    15  		return platforms, errors.Wrap(err, UnmarshalFailedMsg)
    16  	}
    17  	return platforms, nil
    18  }
    19  
    20  // ConvertTypes - convert schema type
    21  func ConvertTypes(iCfg mta.MTA, eCfg Platforms, targetPlatform string) {
    22  	tpl := platformConfig(eCfg, targetPlatform)
    23  	for i, v := range iCfg.Modules {
    24  		moduleAcc := -1
    25  		modulePlatformType := v.Type
    26  		for _, em := range tpl.Modules {
    27  			if ok, acc := satisfiesModuleConfig(v, &em); ok && acc > moduleAcc {
    28  				modulePlatformType = em.PlatformType
    29  				moduleAcc = acc
    30  			}
    31  		}
    32  		iCfg.Modules[i].Type = modulePlatformType
    33  	}
    34  }
    35  
    36  // Satisfies checks if the module m satisfies the conditions defined in the configuration mc.
    37  //
    38  // If it doesn't satisfy the conditions, ok will be false and accuracy will be less than 0.
    39  //
    40  // If it satisfies the conditions, accuracy will be higher the more conditions there are inside the configuration
    41  // (in other words, the more specific match is considered more accurate).
    42  func satisfiesModuleConfig(m *mta.Module, mc *Properties) (ok bool, accuracy int) {
    43  	if m.Type != mc.NativeType {
    44  		return false, -1
    45  	}
    46  	for ckey, cval := range mc.Parameters {
    47  		if mval, ok := m.Parameters[ckey]; !ok || mval != cval {
    48  			return false, -1
    49  		}
    50  	}
    51  	for ckey, cval := range mc.Properties {
    52  		if mval, ok := m.Properties[ckey]; !ok || mval != cval {
    53  			return false, -1
    54  		}
    55  	}
    56  	return true, len(mc.Parameters) + len(mc.Properties)
    57  }
    58  
    59  func platformConfig(eCfg Platforms, targetPlatform string) Modules {
    60  	var tpl Modules
    61  	for _, tp := range eCfg.Platforms {
    62  		if tp.Name == targetPlatform {
    63  			tpl.Name = tp.Name
    64  			tpl.Modules = tp.Modules
    65  		}
    66  	}
    67  	return tpl
    68  }