@@ -11,7 +11,8 @@ import (
11
11
"sort"
12
12
"strings"
13
13
14
- "github.com/crossplane/crossplane-runtime/pkg/fieldpath"
14
+ "github.com/crossplane/upjet/pkg/schema/traverser"
15
+
15
16
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
16
17
twtypes "github.com/muvaf/typewriter/pkg/types"
17
18
"github.com/pkg/errors"
@@ -139,7 +140,7 @@ func (g *Builder) buildResource(res *schema.Resource, cfg *config.Resource, tfPa
139
140
r := & resource {}
140
141
for _ , snakeFieldName := range keys {
141
142
var reference * config.Reference
142
- cPath := fieldPath (append (tfPath , snakeFieldName ))
143
+ cPath := traverser . FieldPath (append (tfPath , snakeFieldName ))
143
144
ref , ok := cfg .References [cPath ]
144
145
// if a reference is configured and the field does not belong to status
145
146
if ok && ! IsObservation (res .Schema [snakeFieldName ]) {
@@ -239,7 +240,7 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, cp
239
240
case schema .TypeString :
240
241
elemType = types .Universe .Lookup ("string" ).Type ()
241
242
case schema .TypeMap , schema .TypeList , schema .TypeSet , schema .TypeInvalid :
242
- return nil , nil , errors .Errorf ("element type of %s is basic but not one of known basic types" , fieldPath (names ))
243
+ return nil , nil , errors .Errorf ("element type of %s is basic but not one of known basic types" , traverser . FieldPath (names ))
243
244
}
244
245
initElemType = elemType
245
246
case * schema.Schema :
@@ -258,14 +259,14 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, cp
258
259
}
259
260
paramType , obsType , initType , err := g .buildResource (et , cfg , f .TerraformPaths , f .CRDPaths , asBlocksMode , names ... )
260
261
if err != nil {
261
- return nil , nil , errors .Wrapf (err , "cannot infer type from resource schema of element type of %s" , fieldPath (names ))
262
+ return nil , nil , errors .Wrapf (err , "cannot infer type from resource schema of element type of %s" , traverser . FieldPath (names ))
262
263
}
263
264
initElemType = initType
264
265
265
266
switch {
266
267
case IsObservation (f .Schema ):
267
268
if obsType == nil {
268
- return nil , nil , errors .Errorf ("element type of %s is computed but the underlying schema does not return observation type" , fieldPath (names ))
269
+ return nil , nil , errors .Errorf ("element type of %s is computed but the underlying schema does not return observation type" , traverser . FieldPath (names ))
269
270
}
270
271
elemType = obsType
271
272
// There are some types that are computed and not optional (observation field) but also has nested fields
@@ -285,7 +286,7 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, cp
285
286
}
286
287
default :
287
288
if paramType == nil {
288
- return nil , nil , errors .Errorf ("element type of %s is configurable but the underlying schema does not return a parameter type" , fieldPath (names ))
289
+ return nil , nil , errors .Errorf ("element type of %s is configurable but the underlying schema does not return a parameter type" , traverser . FieldPath (names ))
289
290
}
290
291
elemType = paramType
291
292
// There are some types that are parameter field but also has nested fields that can go under status.
@@ -308,7 +309,7 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, cp
308
309
elemType = types .Universe .Lookup ("string" ).Type ()
309
310
initElemType = elemType
310
311
default :
311
- return nil , nil , errors .Errorf ("element type of %s should be either schema.Resource or schema.Schema" , fieldPath (names ))
312
+ return nil , nil , errors .Errorf ("element type of %s should be either schema.Resource or schema.Schema" , traverser . FieldPath (names ))
312
313
}
313
314
314
315
// if the singleton list is to be replaced by an embedded object
@@ -339,19 +340,19 @@ type TypeNames struct {
339
340
func NewTypeNames (fieldPaths []string , pkg * types.Package , overrideFieldNames map [string ]string ) (* TypeNames , error ) {
340
341
paramTypeName , err := generateTypeName ("Parameters" , pkg , overrideFieldNames , fieldPaths ... )
341
342
if err != nil {
342
- return nil , errors .Wrapf (err , "cannot generate parameters type name of %s" , fieldPath (fieldPaths ))
343
+ return nil , errors .Wrapf (err , "cannot generate parameters type name of %s" , traverser . FieldPath (fieldPaths ))
343
344
}
344
345
paramName := types .NewTypeName (token .NoPos , pkg , paramTypeName , nil )
345
346
346
347
initTypeName , err := generateTypeName ("InitParameters" , pkg , overrideFieldNames , fieldPaths ... )
347
348
if err != nil {
348
- return nil , errors .Wrapf (err , "cannot generate init parameters type name of %s" , fieldPath (fieldPaths ))
349
+ return nil , errors .Wrapf (err , "cannot generate init parameters type name of %s" , traverser . FieldPath (fieldPaths ))
349
350
}
350
351
initName := types .NewTypeName (token .NoPos , pkg , initTypeName , nil )
351
352
352
353
obsTypeName , err := generateTypeName ("Observation" , pkg , overrideFieldNames , fieldPaths ... )
353
354
if err != nil {
354
- return nil , errors .Wrapf (err , "cannot generate observation type name of %s" , fieldPath (fieldPaths ))
355
+ return nil , errors .Wrapf (err , "cannot generate observation type name of %s" , traverser . FieldPath (fieldPaths ))
355
356
}
356
357
obsName := types .NewTypeName (token .NoPos , pkg , obsTypeName , nil )
357
358
@@ -518,25 +519,6 @@ func sortedKeys(m map[string]*schema.Schema) []string {
518
519
return keys
519
520
}
520
521
521
- func fieldPath (parts []string ) string {
522
- seg := make (fieldpath.Segments , len (parts ))
523
- for i , p := range parts {
524
- if p == wildcard {
525
- continue
526
- }
527
- seg [i ] = fieldpath .Field (p )
528
- }
529
- return seg .String ()
530
- }
531
-
532
- func fieldPathWithWildcard (parts []string ) string {
533
- seg := make (fieldpath.Segments , len (parts ))
534
- for i , p := range parts {
535
- seg [i ] = fieldpath .Field (p )
536
- }
537
- return seg .String ()
538
- }
539
-
540
522
func sanitizePath (p string ) string {
541
523
for _ , reserved := range celReservedKeywords {
542
524
if p == reserved {
0 commit comments