Skip to content

Commit e00f3e7

Browse files
committed
pr comments
1 parent c4fac95 commit e00f3e7

File tree

1 file changed

+56
-20
lines changed
  • framework/components/dockercompose/chip_ingress_set

1 file changed

+56
-20
lines changed

framework/components/dockercompose/chip_ingress_set/protos.go

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ func RegisterAndFetchProtos(ctx context.Context, client *github.Client, protoSch
7979
framework.L.Info().Msgf("Registering and fetching protos from %d repositories", len(protoSchemaSets))
8080

8181
for _, protoSchemaSet := range protoSchemaSets {
82-
framework.L.Info().Msgf("Processing proto schema set: %s", protoSchemaSet.URI)
82+
framework.L.Debug().Msgf("Processing proto schema set: %s", protoSchemaSet.URI)
8383
if len(protoSchemaSet.ExcludeFiles) > 0 {
84-
framework.L.Info().Msgf("Excluding files: %s", strings.Join(protoSchemaSet.ExcludeFiles, ", "))
84+
framework.L.Debug().Msgf("Excluding files: %s", strings.Join(protoSchemaSet.ExcludeFiles, ", "))
8585
}
8686
if valErr := validateRepoConfiguration(protoSchemaSet); valErr != nil {
8787
return errors.Wrapf(valErr, "invalid repo configuration for schema set: %v", protoSchemaSet)
@@ -129,7 +129,7 @@ func RegisterAndFetchProtos(ctx context.Context, client *github.Client, protoSch
129129
subjects[proto.Path] = subjectMessage
130130
}
131131

132-
registerErr := registerAllWithTopologicalSorting(schemaRegistryURL, protoMap, subjects)
132+
registerErr := registerAllWithTopologicalSorting(schemaRegistryURL, protoMap, subjects, protoSchemaSet.Folders)
133133
if registerErr != nil {
134134
return errors.Wrapf(registerErr, "failed to register protos from %s", protoSchemaSet.URI)
135135
}
@@ -303,7 +303,7 @@ searchLoop:
303303
return nil, fmt.Errorf("no proto files found in %s/%s in folders %s", owner, repository, strings.Join(folders, ", "))
304304
}
305305

306-
framework.L.Info().Msgf("Fetched %d proto files from %s/%s", len(files), owner, repository)
306+
framework.L.Debug().Msgf("Fetched %d proto files from %s/%s", len(files), owner, repository)
307307

308308
saveErr := saveProtoFilesToCache(owner, repository, ref, files)
309309
if saveErr != nil {
@@ -457,6 +457,7 @@ func registerAllWithTopologicalSorting(
457457
schemaRegistryURL string,
458458
protoMap map[string]string, // path -> proto source
459459
subjectMap map[string]string, // path -> subject
460+
folders []string, // folders configuration used to determine import prefix transformations
460461
) error {
461462
framework.L.Info().Msgf("Registering %d protobuf schemas", len(protoMap))
462463

@@ -471,7 +472,7 @@ func registerAllWithTopologicalSorting(
471472
return errors.Wrap(sortErr, "failed to sort files topologically")
472473
}
473474

474-
framework.L.Info().Msgf("Registration order (topologically sorted): %v", sortedFiles)
475+
framework.L.Debug().Msgf("Registration order (topologically sorted): %v", sortedFiles)
475476

476477
schemas := map[string]*schemaStatus{}
477478
for path, src := range protoMap {
@@ -495,18 +496,18 @@ func registerAllWithTopologicalSorting(
495496
return fmt.Errorf("no subject found for %s", path)
496497
}
497498

499+
// Determine which folder prefixes should be stripped based on configuration
500+
prefixesToStrip := determineFolderPrefixesToStrip(folders)
501+
498502
// Build references only for files that have dependencies
499503
var fileRefs []map[string]any
500504
if deps, hasDeps := dependencies[path]; hasDeps && len(deps) > 0 {
501505
for _, dep := range deps {
502506
if depSubject, depExists := subjectMap[dep]; depExists {
503-
// The schema registry expects import names without the 'workflows/' prefix
504-
// So if the import is "workflows/v1/metadata.proto", the name should be "v1/metadata.proto"
505-
// And if the import is "workflows/v2/cre_info.proto", the name should be "v2/cre_info.proto"
506-
importName := dep
507-
if strings.HasPrefix(dep, "workflows/") {
508-
importName = strings.TrimPrefix(dep, "workflows/")
509-
}
507+
// The schema registry expects import names without the configured folder prefixes
508+
// So if folders=["workflows"] and the import is "workflows/v1/metadata.proto",
509+
// the name should be "v1/metadata.proto"
510+
importName := stripFolderPrefix(dep, prefixesToStrip)
510511

511512
fileRefs = append(fileRefs, map[string]any{
512513
"name": importName,
@@ -519,16 +520,15 @@ func registerAllWithTopologicalSorting(
519520

520521
// Check if schema is already registered
521522
if existingID, exists := checkSchemaExists(schemaRegistryURL, subject); exists {
522-
framework.L.Info().Msgf("Schema %s already exists with ID %d, skipping registration", subject, existingID)
523+
framework.L.Debug().Msgf("Schema %s already exists with ID %d, skipping registration", subject, existingID)
523524
schema.Registered = true
524525
schema.Version = existingID
525526
continue
526527
}
527528

528-
// The schema registry expects import statements without the 'workflows/' prefix
529-
// So we need to modify the protobuf content to replace "workflows/v1/..." with "v1/..." and "workflows/v2/..." with "v2/..."
530-
modifiedSchema := strings.ReplaceAll(schema.Source, `"workflows/v1/`, `"v1/`)
531-
modifiedSchema = strings.ReplaceAll(modifiedSchema, `"workflows/v2/`, `"v2/`)
529+
// The schema registry expects import statements without the configured folder prefixes
530+
// Transform the schema content to remove these prefixes from import statements
531+
modifiedSchema := transformSchemaContent(schema.Source, prefixesToStrip)
532532

533533
_, registerErr := registerSingleProto(schemaRegistryURL, subject, modifiedSchema, fileRefs)
534534
if registerErr != nil {
@@ -618,11 +618,44 @@ func registerSingleProto(
618618
return result.ID, nil
619619
}
620620

621+
// determineFolderPrefixesToStrip determines which folder prefixes should be stripped from import paths
622+
// based on the folders configuration. The schema registry expects import names to be relative to the
623+
// configured folders, so we strip these prefixes to make imports work correctly.
624+
func determineFolderPrefixesToStrip(folders []string) []string {
625+
var prefixes []string
626+
for _, folder := range folders {
627+
// Ensure folder ends with / for prefix matching
628+
prefix := strings.TrimSuffix(folder, "/") + "/"
629+
prefixes = append(prefixes, prefix)
630+
}
631+
return prefixes
632+
}
633+
634+
// stripFolderPrefix removes any configured folder prefixes from the given path
635+
func stripFolderPrefix(path string, prefixes []string) string {
636+
for _, prefix := range prefixes {
637+
if strings.HasPrefix(path, prefix) {
638+
return strings.TrimPrefix(path, prefix)
639+
}
640+
}
641+
return path
642+
}
643+
644+
// transformSchemaContent removes folder prefixes from import statements in protobuf source
645+
func transformSchemaContent(content string, prefixes []string) string {
646+
modified := content
647+
for _, prefix := range prefixes {
648+
// Transform import statements like "workflows/v1/" to "v1/"
649+
modified = strings.ReplaceAll(modified, `"`+prefix, `"`)
650+
}
651+
return modified
652+
}
653+
621654
// buildDependencyGraph builds a dependency graph from protobuf files
622655
func buildDependencyGraph(protoMap map[string]string) (map[string][]string, error) {
623656
dependencies := make(map[string][]string)
624657

625-
framework.L.Info().Msgf("Building dependency graph for %d proto files", len(protoMap))
658+
framework.L.Debug().Msgf("Building dependency graph for %d proto files", len(protoMap))
626659

627660
// Initialize dependencies map
628661
for path := range protoMap {
@@ -641,9 +674,12 @@ func buildDependencyGraph(protoMap map[string]string) (map[string][]string, erro
641674

642675
// Check if this import exists in our protoMap
643676
if _, exists := protoMap[importPath]; exists {
644-
// Check for self-reference
677+
// Check for self-reference - this indicates either an invalid proto file
678+
// or a potential bug in our import/path handling
645679
if importPath == path {
646-
framework.L.Warn().Msgf("Self-reference detected: %s imports itself!", path)
680+
framework.L.Warn().Msgf("Self-reference detected: file %s imports itself (import: %s). This suggests either an invalid proto file or a path normalization issue. Skipping this dependency to avoid cycles.", path, importPath)
681+
// Continue without adding the dependency to avoid cycles, but don't fail registration
682+
// as this might be a recoverable issue or edge case
647683
continue
648684
}
649685

0 commit comments

Comments
 (0)