package documenttext import ( "log/slog" "github.com/aws/aws-sdk-go-v2/service/textract/types" "github.com/google/uuid" ) func (s *Service) getDirectChildren(block types.Block, blockMap map[uuid.UUID]types.Block) []uuid.UUID { lines := []uuid.UUID{} found := map[uuid.UUID]*bool{} for _, id := range s.getChildIds(block) { if found[id] != nil && !*found[id] { continue } lines = append(lines, id) f := true found[id] = &f s.removeChildren(blockMap[id], blockMap, &found) } output := []uuid.UUID{} for _, id := range lines { if found[id] == nil || !*found[id] { continue } output = append(output, id) } return output } func (s *Service) removeChildren(block types.Block, blockMap map[uuid.UUID]types.Block, found *map[uuid.UUID]*bool) { for _, childId := range s.getChildIds(block) { f := false (*found)[childId] = &f s.removeChildren(blockMap[childId], blockMap, found) } } func (s *Service) getBlockMap(blocksList []types.Block) map[uuid.UUID]types.Block { blocks := make(map[uuid.UUID]types.Block, len(blocksList)) for _, block := range blocksList { if block.Id == nil { continue } id, err := uuid.Parse(*block.Id) if err != nil { slog.Warn("error parsing block id", "error", err, "id", *block.Id) continue } blocks[id] = block } return blocks } func (s *Service) getChildIds(block types.Block) []uuid.UUID { childIds := []uuid.UUID{} for _, relationship := range block.Relationships { for _, idStr := range relationship.Ids { id, err := uuid.Parse(idStr) if err != nil { slog.Error("invalid child uuid", "id", id, "error", err) continue } childIds = append(childIds, id) } } return childIds }