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"
@@ -701,92 +701,41 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
701701// We cannot leverage on the standard marshal/unmarshal because the steps attribute needs to maintain the order of elements
702702// while by default the standard function doesn't do it because in JSON maps are unordered
703703func extractSpecAttributesFromOriginalYamlString (originalYamlString string , pipeline * cfClient.Pipeline ) {
704- // Use mapSlice to preserve order of items from the YAML string
705- m := yaml.MapSlice {}
706- err := yaml .Unmarshal ([]byte (originalYamlString ), & m )
704+ ms := OrderedMapSlice {}
705+ err := yaml .Unmarshal ([]byte (originalYamlString ), & ms )
707706 if err != nil {
708707 log .Fatalf ("Unable to unmarshall original_yaml_string. Error: %v" , err )
709708 }
710709
711710 stages := "[]"
712- // Dynamically build JSON object for steps using String builder
713- stepsBuilder := strings.Builder {}
714- stepsBuilder .WriteString ("{" )
715- // Dynamically build JSON object for steps using String builder
716- hooksBuilder := strings.Builder {}
717- hooksBuilder .WriteString ("{" )
718-
719- // Parse elements of the YAML string to extract Steps and Stages if defined
720- for _ , item := range m {
711+ steps := "{}"
712+ hooks := "{}"
713+
714+ // Parse elements of the YAML string to extract Steps, Hooks and Stages if defined
715+ for _ , item := range ms {
721716 key := item .Key .(string )
722717 switch key {
723718 case "steps" :
724719 switch x := item .Value .(type ) {
725720 default :
726721 log .Fatalf ("unsupported value type: %T" , item .Value )
727722
728- case yaml.MapSlice :
729- numberOfSteps := len (x )
730- for index , item := range x {
731- // We only need to preserve order at the first level to guarantee order of the steps, hence the child nodes can be marshalled
732- // with the standard library
733- y , _ := yaml .Marshal (item .Value )
734- j2 , _ := ghodss .YAMLToJSON (y )
735- stepsBuilder .WriteString ("\" " + item .Key .(string ) + "\" : " + string (j2 ))
736- if index < numberOfSteps - 1 {
737- stepsBuilder .WriteString ("," )
738- }
739- }
723+ case OrderedMapSlice :
724+ s , _ := json .Marshal (x )
725+ steps = string (s )
740726 }
741727 case "stages" :
742- // For Stages we don't have ordering issue because it's a list
743- y , _ := yaml .Marshal (item .Value )
744- j2 , _ := ghodss .YAMLToJSON (y )
745- stages = string (j2 )
728+ s , _ := json .Marshal (item .Value )
729+ stages = string (s )
730+
746731 case "hooks" :
747- switch hooks := item .Value .(type ) {
732+ switch x := item .Value .(type ) {
748733 default :
749734 log .Fatalf ("unsupported value type: %T" , item .Value )
750735
751- case yaml.MapSlice :
752- numberOfHooks := len (hooks )
753- for indexHook , hook := range hooks {
754- // E.g. on_finish
755- hooksBuilder .WriteString ("\" " + hook .Key .(string ) + "\" : {" )
756- numberOfAttributes := len (hook .Value .(yaml.MapSlice ))
757- for indexAttribute , hookAttribute := range hook .Value .(yaml.MapSlice ) {
758- attribute := hookAttribute .Key .(string )
759- switch attribute {
760- case "steps" :
761- hooksBuilder .WriteString ("\" steps\" : {" )
762- numberOfSteps := len (hookAttribute .Value .(yaml.MapSlice ))
763- for indexStep , step := range hookAttribute .Value .(yaml.MapSlice ) {
764- // We only need to preserve order at the first level to guarantee order of the steps, hence the child nodes can be marshalled
765- // with the standard library
766- y , _ := yaml .Marshal (step .Value )
767- j2 , _ := ghodss .YAMLToJSON (y )
768- hooksBuilder .WriteString ("\" " + step .Key .(string ) + "\" : " + string (j2 ))
769- if indexStep < numberOfSteps - 1 {
770- hooksBuilder .WriteString ("," )
771- }
772- }
773- hooksBuilder .WriteString ("}" )
774- default :
775- // For Other elements we don't need to preserve order
776- y , _ := yaml .Marshal (hookAttribute .Value )
777- j2 , _ := ghodss .YAMLToJSON (y )
778- hooksBuilder .WriteString ("\" " + hookAttribute .Key .(string ) + "\" : " + string (j2 ))
779- }
780-
781- if indexAttribute < numberOfAttributes - 1 {
782- hooksBuilder .WriteString ("," )
783- }
784- }
785- hooksBuilder .WriteString ("}" )
786- if indexHook < numberOfHooks - 1 {
787- hooksBuilder .WriteString ("," )
788- }
789- }
736+ case OrderedMapSlice :
737+ h , _ := json .Marshal (x )
738+ hooks = string (h )
790739 }
791740 case "mode" :
792741 pipeline .Spec .Mode = item .Value .(string )
@@ -799,10 +748,7 @@ func extractSpecAttributesFromOriginalYamlString(originalYamlString string, pipe
799748 log .Printf ("Unsupported entry %s" , key )
800749 }
801750 }
802- stepsBuilder .WriteString ("}" )
803- hooksBuilder .WriteString ("}" )
804- steps := stepsBuilder .String ()
805- hooks := hooksBuilder .String ()
751+
806752 pipeline .Spec .Steps = & cfClient.Steps {
807753 Steps : steps ,
808754 }
0 commit comments