1
1
package codefresh
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"log"
6
7
"regexp"
7
8
"strings"
8
9
9
10
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client"
10
- ghodss "github.com/ghodss/yaml"
11
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
13
13
"gopkg.in/yaml.v2"
@@ -752,92 +752,41 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
752
752
// We cannot leverage on the standard marshal/unmarshal because the steps attribute needs to maintain the order of elements
753
753
// while by default the standard function doesn't do it because in JSON maps are unordered
754
754
func 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 )
758
757
if err != nil {
759
758
log .Fatalf ("Unable to unmarshall original_yaml_string. Error: %v" , err )
760
759
}
761
760
762
761
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 {
772
767
key := item .Key .(string )
773
768
switch key {
774
769
case "steps" :
775
770
switch x := item .Value .(type ) {
776
771
default :
777
772
log .Fatalf ("unsupported value type: %T" , item .Value )
778
773
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 )
791
777
}
792
778
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
+
797
782
case "hooks" :
798
- switch hooks := item .Value .(type ) {
783
+ switch x := item .Value .(type ) {
799
784
default :
800
785
log .Fatalf ("unsupported value type: %T" , item .Value )
801
786
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 )
841
790
}
842
791
case "mode" :
843
792
pipeline .Spec .Mode = item .Value .(string )
@@ -850,10 +799,7 @@ func extractSpecAttributesFromOriginalYamlString(originalYamlString string, pipe
850
799
log .Printf ("Unsupported entry %s" , key )
851
800
}
852
801
}
853
- stepsBuilder .WriteString ("}" )
854
- hooksBuilder .WriteString ("}" )
855
- steps := stepsBuilder .String ()
856
- hooks := hooksBuilder .String ()
802
+
857
803
pipeline .Spec .Steps = & cfClient.Steps {
858
804
Steps : steps ,
859
805
}
0 commit comments