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"
@@ -701,92 +701,41 @@ func mapResourceToPipeline(d *schema.ResourceData) *cfClient.Pipeline {
701
701
// We cannot leverage on the standard marshal/unmarshal because the steps attribute needs to maintain the order of elements
702
702
// while by default the standard function doesn't do it because in JSON maps are unordered
703
703
func 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 )
707
706
if err != nil {
708
707
log .Fatalf ("Unable to unmarshall original_yaml_string. Error: %v" , err )
709
708
}
710
709
711
710
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 {
721
716
key := item .Key .(string )
722
717
switch key {
723
718
case "steps" :
724
719
switch x := item .Value .(type ) {
725
720
default :
726
721
log .Fatalf ("unsupported value type: %T" , item .Value )
727
722
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 )
740
726
}
741
727
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
+
746
731
case "hooks" :
747
- switch hooks := item .Value .(type ) {
732
+ switch x := item .Value .(type ) {
748
733
default :
749
734
log .Fatalf ("unsupported value type: %T" , item .Value )
750
735
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 )
790
739
}
791
740
case "mode" :
792
741
pipeline .Spec .Mode = item .Value .(string )
@@ -799,10 +748,7 @@ func extractSpecAttributesFromOriginalYamlString(originalYamlString string, pipe
799
748
log .Printf ("Unsupported entry %s" , key )
800
749
}
801
750
}
802
- stepsBuilder .WriteString ("}" )
803
- hooksBuilder .WriteString ("}" )
804
- steps := stepsBuilder .String ()
805
- hooks := hooksBuilder .String ()
751
+
806
752
pipeline .Spec .Steps = & cfClient.Steps {
807
753
Steps : steps ,
808
754
}
0 commit comments