@@ -22,12 +22,30 @@ type Normalization struct {
22
22
XpathSuffix []string `json:"xpath_suffix" yaml:"xpath_suffix"`
23
23
Locations map [string ]* Location `json:"locations" yaml:"locations"`
24
24
Entry * Entry `json:"entry" yaml:"entry"`
25
- Imports map [ string ] * Import `json:"imports" yaml:"imports"`
25
+ Imports [] Import `json:"imports" yaml:"imports"`
26
26
Version string `json:"version" yaml:"version"`
27
27
Spec * Spec `json:"spec" yaml:"spec"`
28
28
Const map [string ]* Const `json:"const" yaml:"const"`
29
29
}
30
30
31
+ type Import struct {
32
+ Variant * NameVariant
33
+ Type * NameVariant
34
+ Locations map [string ]ImportLocation
35
+ }
36
+
37
+ type ImportLocation struct {
38
+ Name * NameVariant
39
+ XpathElements []string
40
+ XpathVariables map [string ]ImportXpathVariable
41
+ }
42
+
43
+ type ImportXpathVariable struct {
44
+ Name * NameVariant
45
+ Description string
46
+ Default string
47
+ }
48
+
31
49
type TerraformResourceType string
32
50
33
51
const (
@@ -90,19 +108,6 @@ type Entry struct {
90
108
Name * EntryName `json:"name" yaml:"name"`
91
109
}
92
110
93
- type Import struct {
94
- Name * NameVariant
95
- Xpath []string `json:"xpath" yaml:"xpath"`
96
- Vars map [string ]* ImportVar `json:"vars" yaml:"vars"`
97
- OnlyForParams []string `json:"only_for_params" yaml:"only_for_params"`
98
- }
99
-
100
- type ImportVar struct {
101
- Name * NameVariant
102
- Description string `json:"description" yaml:"description"`
103
- Default string `json:"default" yaml:"default"`
104
- }
105
-
106
111
type EntryName struct {
107
112
Description string `json:"description" yaml:"description"`
108
113
Length * EntryNameLength `json:"length" yaml:"length"`
@@ -447,19 +452,6 @@ func generateXpathVariables(variables []xpathschema.Variable) map[string]*Locati
447
452
return xpathVars
448
453
}
449
454
450
- func generateImportVariables (variables []xpathschema.Variable ) map [string ]* ImportVar {
451
- importVars := make (map [string ]* ImportVar )
452
- for _ , variable := range variables {
453
- entry := & ImportVar {
454
- Description : variable .Description ,
455
- Default : variable .Default ,
456
- }
457
- importVars [variable .Name ] = entry
458
- }
459
-
460
- return importVars
461
- }
462
-
463
455
func schemaToSpec (object object.Object ) (* Normalization , error ) {
464
456
var resourceVariants []TerraformResourceVariant
465
457
for _ , elt := range object .TerraformConfig .ResourceVariants {
@@ -478,7 +470,6 @@ func schemaToSpec(object object.Object) (*Normalization, error) {
478
470
PluralName : object .TerraformConfig .PluralName ,
479
471
},
480
472
Locations : make (map [string ]* Location ),
481
- Imports : make (map [string ]* Import ),
482
473
GoSdkPath : object .GoSdkConfig .Package ,
483
474
XpathSuffix : object .XpathSuffix ,
484
475
Version : object .Version ,
@@ -560,39 +551,52 @@ func schemaToSpec(object object.Object) (*Normalization, error) {
560
551
561
552
}
562
553
563
- imports := make ( map [ string ] * Import , len ( object . Imports ))
554
+ var imports [] Import
564
555
for _ , elt := range object .Imports {
565
- var xpath [] string
566
-
567
- schemaXpathVars := make (map [string ]xpathschema.Variable )
568
- for _ , xpathVariable := range elt .Xpath .Variables {
569
- schemaXpathVars [ xpathVariable . Name ] = xpathVariable
570
- }
571
-
572
- for _ , element := range elt . Xpath . Elements {
573
- var eltEntry string
574
- if xpathVar , ok := schemaXpathVars [ element [ 1 :]]; ok {
575
- if xpathVar . Type == "entry" {
576
- eltEntry = fmt . Sprintf ( "{{ Entry %s }}" , elt . Name )
577
- } else if xpathVar . Type == "object" {
578
- eltEntry = fmt . Sprintf ( "{{ Object %s }}" , elt . Name )
556
+ locations := make ( map [ string ] ImportLocation , len ( elt . Locations ))
557
+ for _ , location := range elt . Locations {
558
+ schemaXpathVars := make (map [string ]xpathschema.Variable , len ( location . Xpath . Variables ) )
559
+ xpathVars := make ( map [ string ] ImportXpathVariable , len ( location .Xpath .Variables ))
560
+ for _ , xpathVariable := range location . Xpath . Variables {
561
+ schemaXpathVars [ xpathVariable . Name ] = xpathVariable
562
+ xpathVars [ xpathVariable . Name ] = ImportXpathVariable {
563
+ Name : & NameVariant {
564
+ Underscore : naming . Underscore ( "" , xpathVariable . Name , "" ),
565
+ CamelCase : naming . CamelCase ( "" , xpathVariable . Name , "" , true ),
566
+ LowerCamelCase : naming . CamelCase ( "" , xpathVariable . Name , "" , false ),
567
+ },
568
+ Description : xpathVariable . Description ,
569
+ Default : xpathVariable . Default ,
579
570
}
580
- } else {
581
- eltEntry = element
582
571
}
583
- xpath = append (xpath , eltEntry )
584
- }
585
572
586
- importVariables := generateImportVariables (elt .Xpath .Variables )
587
- if len (importVariables ) == 0 {
588
- importVariables = nil
589
- }
573
+ var xpath []string
574
+ xpath = append (xpath , location .Xpath .Elements ... )
590
575
591
- imports [elt .Name ] = & Import {
592
- Xpath : xpath ,
593
- Vars : importVariables ,
594
- OnlyForParams : elt .OnlyForParams ,
576
+ locations [location .Name ] = ImportLocation {
577
+ Name : & NameVariant {
578
+ Underscore : naming .Underscore ("" , location .Name , "" ),
579
+ CamelCase : naming .CamelCase ("" , location .Name , "" , true ),
580
+ LowerCamelCase : naming .CamelCase ("" , location .Name , "" , false ),
581
+ },
582
+ XpathVariables : xpathVars ,
583
+ XpathElements : xpath ,
584
+ }
595
585
}
586
+
587
+ imports = append (imports , Import {
588
+ Type : & NameVariant {
589
+ Underscore : naming .Underscore ("" , elt .Type , "" ),
590
+ CamelCase : naming .CamelCase ("" , elt .Type , "" , true ),
591
+ LowerCamelCase : naming .CamelCase ("" , elt .Type , "" , false ),
592
+ },
593
+ Variant : & NameVariant {
594
+ Underscore : naming .Underscore ("" , elt .Variant , "" ),
595
+ CamelCase : naming .CamelCase ("" , elt .Variant , "" , true ),
596
+ LowerCamelCase : naming .CamelCase ("" , elt .Variant , "" , false ),
597
+ },
598
+ Locations : locations ,
599
+ })
596
600
}
597
601
598
602
if len (imports ) > 0 {
@@ -660,11 +664,6 @@ func ParseSpec(input []byte) (*Normalization, error) {
660
664
return nil , err
661
665
}
662
666
663
- err = spec .AddNameVariantsForImports ()
664
- if err != nil {
665
- return nil , err
666
- }
667
-
668
667
err = spec .AddNameVariantsForParams ()
669
668
if err != nil {
670
669
return nil , err
@@ -704,27 +703,6 @@ func (spec *Normalization) AddNameVariantsForLocation() error {
704
703
return nil
705
704
}
706
705
707
- // AddNameVariantsForImports add name variants for imports (under_score and CamelCase).
708
- func (spec * Normalization ) AddNameVariantsForImports () error {
709
- for key , imp := range spec .Imports {
710
- imp .Name = & NameVariant {
711
- Underscore : naming .Underscore ("" , key , "" ),
712
- CamelCase : naming .CamelCase ("" , key , "" , true ),
713
- LowerCamelCase : naming .CamelCase ("" , key , "" , false ),
714
- }
715
-
716
- for subkey , variable := range imp .Vars {
717
- variable .Name = & NameVariant {
718
- Underscore : naming .Underscore ("" , subkey , "" ),
719
- CamelCase : naming .CamelCase ("" , subkey , "" , true ),
720
- LowerCamelCase : naming .CamelCase ("" , subkey , "" , false ),
721
- }
722
- }
723
- }
724
-
725
- return nil
726
- }
727
-
728
706
// AddNameVariantsForParams recursively add name variants for params for nested specs.
729
707
func AddNameVariantsForParams (name string , param * SpecParam ) error {
730
708
param .Name = & NameVariant {
0 commit comments