Skip to content

Commit

Permalink
more generic HasVSIHandler
Browse files Browse the repository at this point in the history
it covers the case where prefix is in handlers but handler is nil

Co-authored-by: Thomas Bonfort <thomas.bonfort@airbus.com>
  • Loading branch information
vincentvaroquauxads and tbonfort committed Sep 11, 2024
1 parent 89f29df commit 2717b58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 1 addition & 5 deletions godal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3900,11 +3900,7 @@ func RegisterVSIHandler(prefix string, handler KeySizerReaderAt, opts ...VSIHand

// HasVSIHandler returns true if a VSIHandler is registered for this prefix
func HasVSIHandler(prefix string) bool {
if handlers == nil {
return false
}
_, ok := handlers[prefix]
return ok
return handlers != nil && handlers[prefix].KeySizerReaderAt != nil
}

// BuildVRT runs the GDALBuildVRT function and creates a VRT dataset from a list of datasets
Expand Down
18 changes: 16 additions & 2 deletions godal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3459,17 +3459,31 @@ func (mvp mvpHandler) ReadAtMulti(k string, buf [][]byte, off []int64) ([]int, e
}
return b.(KeyMultiReader).ReadAtMulti(k, buf, off)
}
func TestHasVSIHandler(t *testing.T) { // stripPrefix false
assert.False(t, HasVSIHandler("unregistered_prefix://"))

vpa := vpHandler{datas: make(map[string]KeySizerReaderAt)}
err := RegisterVSIHandler("registered_prefix://", vpa, VSIHandlerStripPrefix(false))
assert.NoError(t, err)
assert.True(t, HasVSIHandler("registered_prefix://"))

// nil handler
err = RegisterVSIHandler("nil_prefix://", nil)
assert.NoError(t, err)
assert.False(t, HasVSIHandler("nil_prefix://"))

// unregistered_prefix
assert.False(t, HasVSIHandler("unregistered_prefix://"))
}

func TestVSIPrefix(t *testing.T) {
tifdat, _ := ioutil.ReadFile("testdata/test.tif")

assert.False(t, HasVSIHandler("prefix://"))
// stripPrefix false
vpa := vpHandler{datas: make(map[string]KeySizerReaderAt)}
vpa.datas["prefix://test.tif"] = mbufHandler{tifdat}
err := RegisterVSIHandler("prefix://", vpa, VSIHandlerStripPrefix(false))
assert.NoError(t, err)
assert.True(t, HasVSIHandler("prefix://"))

ds, err := Open("prefix://test.tif")
if err != nil {
Expand Down

0 comments on commit 2717b58

Please sign in to comment.