MCPcopy Create free account
hub / github.com/bufbuild/buf / reparseImageProto

Function reparseImageProto

private/bufpkg/bufimage/bufimage.go:672–714  ·  view source on GitHub ↗
(protoImage *imagev1.Image, resolver protoencoding.Resolver, computeUnusedImports bool)

Source from the content-addressed store, hash-verified

670}
671
672func reparseImageProto(protoImage *imagev1.Image, resolver protoencoding.Resolver, computeUnusedImports bool) error {
673 if err := protoencoding.ReparseExtensions(resolver, protoImage.ProtoReflect()); err != nil {
674 return fmt.Errorf("could not reparse image: %v", err)
675 }
676 if computeUnusedImports {
677 tracker := &importTracker{
678 resolver: resolver,
679 used: map[string]map[string]struct{}{},
680 }
681 tracker.findUsedImports(protoImage)
682 // Now we can populated list of unused dependencies
683 for _, file := range protoImage.GetFile() {
684 bufExt := file.GetBufExtension()
685 if bufExt == nil {
686 bufExt = &imagev1.ImageFileExtension{}
687 file.SetBufExtension(bufExt)
688 }
689 bufExt.SetUnusedDependency(nil) // reset
690 usedImports := tracker.used[file.GetName()]
691 for i, dep := range file.GetDependency() {
692 if _, ok := usedImports[dep]; !ok {
693 // it's fine if it's public
694 isPublic := false
695 for _, publicDepIndex := range file.GetPublicDependency() {
696 if i == int(publicDepIndex) {
697 isPublic = true
698 break
699 }
700 }
701 if !isPublic {
702 // This should never happen, however we do a bounds check to ensure that we are
703 // doing a safe conversion for the index.
704 if i > math.MaxInt32 || i < math.MinInt32 {
705 return fmt.Errorf("unused dependency index out-of-bounds for int32 conversion: %v", i)
706 }
707 bufExt.SetUnusedDependency(append(bufExt.GetUnusedDependency(), int32(i)))
708 }
709 }
710 }
711 }
712 }
713 return nil
714}
715
716// We pass in the pathToImageFileInfo here because we also call this in
717// ImageFileInfosWithOnlyTargetsAndTargetImports and we don't want to have to make this map twice.

Callers 1

NewImageForProtoFunction · 0.85

Calls 11

findUsedImportsMethod · 0.95
ReparseExtensionsFunction · 0.92
GetBufExtensionMethod · 0.80
SetBufExtensionMethod · 0.80
SetUnusedDependencyMethod · 0.80
GetUnusedDependencyMethod · 0.80
GetFileMethod · 0.65
GetNameMethod · 0.65
GetDependencyMethod · 0.65
GetPublicDependencyMethod · 0.65
ProtoReflectMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…