Skip to content

Commit 4dd8a5a

Browse files
committed
hdf5: add ObjectTypeByIndex to CommonFG
This CL adds a new method to CommonFG, ObjectTypeByIndex, to retrieve the type of an object in a file or a group, prior to read from storage. Fixes #77. Signed-off-by: Sebastien Binet <[email protected]>
1 parent eabe91d commit 4dd8a5a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

h5g_group.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ import (
1515
"unsafe"
1616
)
1717

18+
// GType describes the type of an object inside a Group or File.
19+
type GType int
20+
21+
const (
22+
H5G_UNKNOWN GType = C.H5G_UNKNOWN // Unknown object type
23+
H5G_GROUP GType = C.H5G_GROUP // Object is a group
24+
H5G_DATASET GType = C.H5G_DATASET // Object is a dataset
25+
H5G_TYPE GType = C.H5G_TYPE // Object is a named data type
26+
H5G_LINK GType = C.H5G_LINK // Object is a symbolic link
27+
H5G_UDLINK GType = C.H5G_UDLINK // Object is a user-defined link
28+
)
29+
30+
func (typ GType) String() string {
31+
switch typ {
32+
case H5G_UNKNOWN:
33+
return "unknown"
34+
case H5G_GROUP:
35+
return "group"
36+
case H5G_DATASET:
37+
return "dataset"
38+
case H5G_TYPE:
39+
return "type"
40+
case H5G_LINK:
41+
return "link"
42+
case H5G_UDLINK:
43+
return "udlink"
44+
default:
45+
return fmt.Sprintf("GType(%d)", int(typ))
46+
}
47+
}
48+
1849
// CommonFG is for methods common to both File and Group
1950
type CommonFG struct {
2051
Identifier
@@ -145,6 +176,17 @@ func (g *CommonFG) ObjectNameByIndex(idx uint) (string, error) {
145176
return C.GoString(&name[0]), nil
146177
}
147178

179+
// ObjectTypeByIndex returns the type of the object at idx.
180+
func (g *CommonFG) ObjectTypeByIndex(idx uint) (GType, error) {
181+
cidx := C.hsize_t(idx)
182+
gtyp := GType(C.H5Gget_objtype_by_idx(g.id, cidx))
183+
if gtyp < H5G_GROUP {
184+
return H5G_UNKNOWN, fmt.Errorf("could not get object type")
185+
}
186+
187+
return gtyp, nil
188+
}
189+
148190
// CreateTable creates a packet table to store fixed-length packets.
149191
// The returned table must be closed by the user when it is no longer needed.
150192
func (g *Group) CreateTable(name string, dtype *Datatype, chunkSize, compression int) (*Table, error) {

h5g_group_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ func TestGroup(t *testing.T) {
7171
t.Errorf("wrong number of objects in group: want 1, got %d", nObjs)
7272
}
7373

74+
if name, err := g2.ObjectNameByIndex(0); err != nil {
75+
t.Fatalf("could not retrieve object name idx=%d: %+v", 0, err)
76+
} else if got, want := name, "baz"; got != want {
77+
t.Errorf("invalid name for object idx=%d: got=%q, want=%q", 0, got, want)
78+
}
79+
80+
if typ, err := g2.ObjectTypeByIndex(0); err != nil {
81+
t.Fatalf("could not retrieve object type idx=%d: %+v", 0, err)
82+
} else if got, want := typ, H5G_GROUP; got != want {
83+
t.Errorf("invalid type for object idx=%d: got=%v, want=%v", 0, got, want)
84+
}
85+
7486
err = g1.Close()
7587
if err != nil {
7688
t.Error(err)

0 commit comments

Comments
 (0)