Skip to Content
go-dicom(3)
Packages

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

NameWhat it is for
NewHookChain, HookChainFunctions registered per stage, run in order.
HookChain.RegisterHook, HookChain.ExecuteHooksAdd a function to a stage; run a stage on one element.
HookLevel, PreValidationThe stages: before and after validation, compression and serialization.
AdvancedHookFuncThe function type a chain holds.
FilteringHookFunc, TransformingHookFunc, ValidatingHookFuncBuild one from a predicate, a transformer or a validator.
RegisterGlobalChain, GetGlobalChainChains by name, process-wide.
Hooks, RawDataHook, RawDataElementRaw-element callbacks with keyword arguments.
FixSeparatorHook, RetryAlternateVRHookReady-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 ExecuteHooks stops there; handle it as "dropped".
  • GlobalHooks and the raw-element callbacks are not called by any reader in this module.
  • Global chains and GlobalHooks are process-wide state; prefer a HookChain you own.

Note

This package is reached only through the legacy DCMFileReader. For new code, work on the dataset directly.

See also

filereader · filewriter · dataelem · Reading and writing

On this page