From 0580ce227bf7d8cf39e9ebc7216bbfd2ee06ac70 Mon Sep 17 00:00:00 2001 From: Adin Schmahmann Date: Wed, 5 Apr 2023 13:01:48 -0400 Subject: [PATCH] use simple path selectors with implicit reifier instead of ones keyed by name --- lib/graph_gateway.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/graph_gateway.go b/lib/graph_gateway.go index bae89f7..2c619f1 100644 --- a/lib/graph_gateway.go +++ b/lib/graph_gateway.go @@ -12,6 +12,7 @@ import ( "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/node/basicnode" "github.com/ipld/go-ipld-prime/schema" + "github.com/ipld/go-ipld-prime/traversal/selector/builder" "io" "net/http" "runtime" @@ -341,7 +342,7 @@ func (api *GraphGateway) tryLoadPathFromBlockstore(ctx context.Context, p gatewa var lastPath ipld.Path var lastLink ipld.Link var requestPath string - pathSel := unixfsnode.UnixFSPathSelector(strings.Join(rem, "/")) + pathSel := pathAllSelector(rem) if err := fetcherhelpers.BlockMatching(ctx, f, rootCidLink, pathSel, func(result fetcher.FetchResult) error { lastPath = result.LastBlockPath lastLink = result.LastBlockLink @@ -760,3 +761,21 @@ func (b *blockFetcherExchWrapper) Close() error { } var _ exchange.Interface = (*blockFetcherExchWrapper)(nil) + +func pathAllSelector(path []string) ipld.Node { + ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any) + return pathSelector(path, ssb, func(p string, s builder.SelectorSpec) builder.SelectorSpec { + return ssb.ExploreUnion( + ssb.Matcher(), + ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) { efsb.Insert(p, s) }), + ) + }) +} + +func pathSelector(path []string, ssb builder.SelectorSpecBuilder, reduce func(string, builder.SelectorSpec) builder.SelectorSpec) ipld.Node { + spec := ssb.Matcher() + for i := len(path) - 1; i >= 0; i-- { + spec = reduce(path[i], spec) + } + return spec.Node() +}