hooks
Callbacks run on data elements: chains of filter, transform and validate functions at named stages, and raw-element callbacks with keyword arguments.
import "github.com/amrshadid/go-dicom/hooks" · pkg.go.dev
When to use it
Rarely. A chain runs only where a caller runs it: the legacy
filereader.DCMFileReader.RegisterHook runs PostValidation hooks on each
element it reads. filereader.ReadDICOMFile, the data set and the
DICOMFileWriter do not run hooks at all.
To change elements, change them on the dataset after reading
(ForEach, Remove, SetValue) or before writing.
Key types and functions
| Name | What it is for |
|---|---|
NewHookChain, HookChain | Functions registered per stage, run in order. |
HookChain.RegisterHook, HookChain.ExecuteHooks | Add a function to a stage; run a stage on one element. |
HookLevel, PreValidation | The stages: before and after validation, compression and serialization. |
AdvancedHookFunc | The function type a chain holds. |
FilteringHookFunc, TransformingHookFunc, ValidatingHookFunc | Build one from a predicate, a transformer or a validator. |
RegisterGlobalChain, GetGlobalChain | Chains by name, process-wide. |
Hooks, RawDataHook, RawDataElement | Raw-element callbacks with keyword arguments. |
FixSeparatorHook, RetryAlternateVRHook | Ready-made raw callbacks. |
Example
// Run elements through a chain of hooks: drop private tags, upper-case a code.
package main
import (
"fmt"
"log"
"strings"
"github.com/amrshadid/go-dicom/dataelem"
"github.com/amrshadid/go-dicom/hooks"
"github.com/amrshadid/go-dicom/tag"
)
func main() {
chain := hooks.NewHookChain()
err := chain.RegisterHook(hooks.PreSerialization, hooks.FilteringHookFunc(func(e *dataelem.DataElement) bool {
return !e.IsPrivate() // false stops the chain with an error
}))
if err != nil {
log.Fatal(err)
}
err = chain.RegisterHook(hooks.PreSerialization, hooks.TransformingHookFunc(func(e *dataelem.DataElement) (*dataelem.DataElement, error) {
if e.GetVR() == dataelem.CS {
if s, ok := e.GetValue().(string); ok {
e.SetValue(strings.ToUpper(s))
}
}
return e, nil
}))
if err != nil {
log.Fatal(err)
}
elems := []*dataelem.DataElement{
dataelem.NewDataElement(tag.New(0x0008, 0x0060), dataelem.CS, "ct"),
dataelem.NewDataElement(tag.New(0x0029, 0x1010), dataelem.LO, "operator"),
}
for _, e := range elems {
out, err := chain.ExecuteHooks(e, hooks.PreSerialization)
if err != nil {
fmt.Println(e.MustTag(), "dropped:", err)
continue
}
fmt.Println(out.MustTag(), out.GetValue())
}
}It prints (0008,0060) CT and reports the private element as dropped.
Notes
- A filter whose predicate is false returns an error, and
ExecuteHooksstops there; handle it as "dropped". GlobalHooksand the raw-element callbacks are not called by any reader in this module.- Global chains and
GlobalHooksare process-wide state; prefer aHookChainyou own.
Note
This package is reached only through the legacy DCMFileReader. For new
code, work on the dataset directly.
See also
filewriter
Writes DICOM Part 10 files: the preamble, the File Meta group and the data set, encoded in the transfer syntax the meta group names.
jsonrep
A JSON form of its own: a fixed struct of about twenty common attributes (DicomDataset), with helpers to serialize, filter and merge it, and a handler that moves bulk data out to files or URIs.