11package codefresh
22
33import (
4+ "encoding/json"
45 "fmt"
56 "log"
67 "regexp"
78 "strings"
89
910 cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
10- ghodss "github.com/ghodss/yaml"
1111 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1313 "gopkg.in/yaml.v2"
@@ -752,92 +752,41 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
752752// We cannot leverage on the standard marshal/unmarshal because the steps attribute needs to maintain the order of elements
753753// while by default the standard function doesn't do it because in JSON maps are unordered
754754func extractSpecAttributesFromOriginalYamlString (originalYamlString string , pipeline * cfClient.Pipeline ) {
755- // Use mapSlice to preserve order of items from the YAML string
756- m := yaml.MapSlice {}
757- err := yaml .Unmarshal ([]byte (originalYamlString ), & m )
755+ ms := OrderedMapSlice {}
756+ err := yaml .Unmarshal ([]byte (originalYamlString ), & ms )
758757 if err != nil {
759758 log .Fatalf ("Unable to unmarshall original_yaml_string. Error: %v" , err )
760759 }
761760
762761 stages := "[]"
763- // Dynamically build JSON object for steps using String builder
764- stepsBuilder := strings.Builder {}
765- stepsBuilder .WriteString ("{" )
766- // Dynamically build JSON object for steps using String builder
767- hooksBuilder := strings.Builder {}
768- hooksBuilder .WriteString ("{" )
769-
770- // Parse elements of the YAML string to extract Steps and Stages if defined
771- for _ , item := range m {
762+ steps := "{}"
763+ hooks := "{}"
764+
765+ // Parse elements of the YAML string to extract Steps, Hooks and Stages if defined
766+ for _ , item := range ms {
772767 key := item .Key .(string )
773768 switch key {
774769 case "steps" :
775770 switch x := item .Value .(type ) {
776771 default :
777772 log .Fatalf ("unsupported value type: %T" , item .Value )
778773
779- case yaml.MapSlice :
780- numberOfSteps := len (x )
781- for index , item := range x {
782- // We only need to preserve order at the first level to guarantee order of the steps, hence the child nodes can be marshalled
783- // with the standard library
784- y , _ := yaml .Marshal (item .Value )
785- j2 , _ := ghodss .YAMLToJSON (y )
786- stepsBuilder .WriteString ("\" " + item .Key .(string ) + "\" : " + string (j2 ))
787- if index < numberOfSteps - 1 {
788- stepsBuilder .WriteString ("," )
789- }
790- }
774+ case OrderedMapSlice :
775+ s , _ := json .Marshal (x )
776+ steps = string (s )
791777 }
792778 case "stages" :
793- // For Stages we don't have ordering issue because it's a list
794- y , _ := yaml .Marshal (item .Value )
795- j2 , _ := ghodss .YAMLToJSON (y )
796- stages = string (j2 )
779+ s , _ := json .Marshal (item .Value )
780+ stages = string (s )
781+
797782 case "hooks" :
798- switch hooks := item .Value .(type ) {
783+ switch x := item .Value .(type ) {
799784 default :
800785 log .Fatalf ("unsupported value type: %T" , item .Value )
801786
802- case yaml.MapSlice :
803- numberOfHooks := len (hooks )
804- for indexHook , hook := range hooks {
805- // E.g. on_finish
806- hooksBuilder .WriteString ("\" " + hook .Key .(string ) + "\" : {" )
807- numberOfAttributes := len (hook .Value .(yaml.MapSlice ))
808- for indexAttribute , hookAttribute := range hook .Value .(yaml.MapSlice ) {
809- attribute := hookAttribute .Key .(string )
810- switch attribute {
811- case "steps" :
812- hooksBuilder .WriteString ("\" steps\" : {" )
813- numberOfSteps := len (hookAttribute .Value .(yaml.MapSlice ))
814- for indexStep , step := range hookAttribute .Value .(yaml.MapSlice ) {
815- // We only need to preserve order at the first level to guarantee order of the steps, hence the child nodes can be marshalled
816- // with the standard library
817- y , _ := yaml .Marshal (step .Value )
818- j2 , _ := ghodss .YAMLToJSON (y )
819- hooksBuilder .WriteString ("\" " + step .Key .(string ) + "\" : " + string (j2 ))
820- if indexStep < numberOfSteps - 1 {
821- hooksBuilder .WriteString ("," )
822- }
823- }
824- hooksBuilder .WriteString ("}" )
825- default :
826- // For Other elements we don't need to preserve order
827- y , _ := yaml .Marshal (hookAttribute .Value )
828- j2 , _ := ghodss .YAMLToJSON (y )
829- hooksBuilder .WriteString ("\" " + hookAttribute .Key .(string ) + "\" : " + string (j2 ))
830- }
831-
832- if indexAttribute < numberOfAttributes - 1 {
833- hooksBuilder .WriteString ("," )
834- }
835- }
836- hooksBuilder .WriteString ("}" )
837- if indexHook < numberOfHooks - 1 {
838- hooksBuilder .WriteString ("," )
839- }
840- }
787+ case OrderedMapSlice :
788+ h , _ := json .Marshal (x )
789+ hooks = string (h )
841790 }
842791 case "mode" :
843792 pipeline .Spec .Mode = item .Value .(string )
@@ -850,10 +799,7 @@ func extractSpecAttributesFromOriginalYamlString(originalYamlString string, pipe
850799 log .Printf ("Unsupported entry %s" , key )
851800 }
852801 }
853- stepsBuilder .WriteString ("}" )
854- hooksBuilder .WriteString ("}" )
855- steps := stepsBuilder .String ()
856- hooks := hooksBuilder .String ()
802+
857803 pipeline .Spec .Steps = & cfClient.Steps {
858804 Steps : steps ,
859805 }
0 commit comments