Documentation
¶
Overview ¶
Package codex is a format-agnostic in-memory model for MARC 21 bibliographic records, shared by the serialization subpackages.
A Record is a 24-byte Leader plus an ordered list of Fields. A control field (tag below "010") holds raw data; a data field carries two indicators and zero or more Subfields. Every MARC serialization maps onto this one model, so the same record round-trips through any format.
The model is domain-agnostic: it exposes leaders, fields, subfields and indicators, and leaves interpretation of specific tags to the caller.
Serialization lives in subpackages, each implementing RecordReader and RecordWriter:
- iso2709 — the binary ISO 2709 interchange format (.mrc)
- marcxml — the Library of Congress MARCXML slim schema (planned)
- marcjson — the MARC-in-JSON structure (planned)
- mrk — the MARCMaker mnemonic text format (planned)
All values exposed by this package are UTF-8 Go strings; each codec is responsible for transcoding its wire encoding to and from UTF-8.
Index ¶
- func All(r RecordReader) iter.Seq2[*Record, error]
- func Convert(r RecordReader, w RecordWriter) error
- type Field
- type Leader
- type Record
- func (r *Record) AddField(f Field) *Record
- func (r *Record) ControlField(tag string) string
- func (r *Record) DataField(tag string) (Field, bool)
- func (r *Record) DataFields(tag string) []Field
- func (r *Record) Encoding() byte
- func (r *Record) Fields() []Field
- func (r *Record) InsertField(f Field) *Record
- func (r *Record) Leader() Leader
- func (r *Record) RemoveFields(tag string) *Record
- func (r *Record) ReplaceField(f Field) *Record
- func (r *Record) SetLeader(l Leader) *Record
- func (r *Record) SubfieldValue(tag string, code byte) string
- func (r *Record) SubfieldValues(tag string, code byte) []string
- func (r *Record) Validate() error
- type RecordReader
- type RecordWriter
- type Subfield
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All(r RecordReader) iter.Seq2[*Record, error]
All returns an iterator over the records produced by r, for use as
for rec, err := range codex.All(reader) { ... }
It stops at the first error, yielding (nil, err) once; io.EOF ends iteration cleanly without yielding. It works with any RecordReader, so every format shares one iterator.
func Convert ¶
func Convert(r RecordReader, w RecordWriter) error
Convert reads every record from r and writes it to w, returning the first error encountered (io.EOF is the normal end and is not returned). Because it is written against the interfaces, it converts between any two formats. A writer that buffers a wrapper (e.g. a marcxml <collection> or a marcjson array) still needs the caller to call its Close method afterward to finalize the output.
Example ¶
ExampleConvert converts a binary ISO 2709 record to MARCMaker text using only the format-agnostic RecordReader and RecordWriter interfaces.
rec := codex.NewRecord().
AddField(codex.NewControlField("001", "ex-1")).
AddField(codex.NewDataField("245", '0', '0', codex.NewSubfield('a', "Example record")))
var binary bytes.Buffer
if err := iso2709.NewWriter(&binary).Write(rec); err != nil {
log.Fatal(err)
}
if err := codex.Convert(iso2709.NewReader(&binary), mrk.NewWriter(os.Stdout)); err != nil {
log.Fatal(err)
}
Output: =LDR 00074nam a2200049 4500 =001 ex-1 =245 00$aExample record
Types ¶
type Field ¶
Field is a single MARC field. A control field (Tag < "010") carries raw data in Value and has no indicators or subfields. A data field (Tag >= "010") carries two indicators and zero or more subfields. The conventional blank indicator is the ASCII space (' '); an unset (zero) data-field indicator is treated as a blank space when the field is serialized.
func NewControlField ¶
NewControlField constructs a control field (e.g. 001, 003, 008) holding raw data. The tag is not validated here; values below "010" are treated as control fields by the reader and writer.
func NewDataField ¶
NewDataField constructs a data field with the given tag, two indicators and subfields. A blank indicator is conventionally the ASCII space (' ').
func (Field) Indicators ¶
Indicators returns the field's two indicator bytes. For control fields both are zero.
func (Field) Subfield ¶
Subfield returns the value of the first subfield with the given code and reports whether one was found.
func (Field) SubfieldValue ¶
SubfieldValue returns the value of the first subfield with the given code, or the empty string if none is present.
func (Field) SubfieldValues ¶
SubfieldValues returns the values of every subfield with the given code, in order.
type Leader ¶
type Leader string
Leader is the 24-byte MARC record leader. Helper methods decode the fields this package needs; the raw bytes are available via String.
func (Leader) BaseAddress ¶
BaseAddress returns the base address of data declared in leader bytes [12:17]. It returns 0 if those bytes are not a valid number.
func (Leader) BibLevel ¶
BibLevel returns leader byte 7 (the bibliographic level: 'm' monograph, 's' serial, 'a' monographic component part, etc.), or 0 if the leader is malformed.
func (Leader) Encoding ¶
Encoding returns leader byte 9, the character coding scheme: 'a' for UTF-8 (Unicode) or blank for MARC-8.
func (Leader) RecordLength ¶
RecordLength returns the total record length declared in leader bytes [0:5]. It returns 0 if those bytes are not a valid number.
func (Leader) RecordStatus ¶
RecordStatus returns leader byte 5 (the record status), or 0 if the leader is malformed.
func (Leader) RecordType ¶
RecordType returns leader byte 6 (the type of record), or 0 if the leader is malformed.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record is a parsed MARC record: its leader and ordered fields.
func NewRecord ¶
func NewRecord() *Record
NewRecord creates an empty record with a syntactically valid default leader (UTF-8 encoding). Fields are added with AddField.
func NewRecordCap ¶
NewRecordCap creates an empty record like NewRecord but with space preallocated for n fields. Codecs that know the field count up front use it to avoid reallocating the field slice while appending.
func (*Record) AddField ¶
AddField appends a field to the record and returns the record for chaining.
func (*Record) ControlField ¶
ControlField returns the raw value of the first control field with the given tag, or the empty string if none is present.
func (*Record) DataField ¶
DataField returns the first data field with the given tag and reports whether one was found.
func (*Record) DataFields ¶
DataFields returns every data field with the given tag, in order.
func (*Record) Encoding ¶
Encoding returns the record's declared character encoding (leader byte 9).
func (*Record) InsertField ¶
InsertField inserts f keeping the record ordered by ascending tag: f is placed after any existing fields with a tag less than or equal to f's. It returns the record. Insert into an already tag-ordered record to keep it ordered.
func (*Record) RemoveFields ¶
RemoveFields removes every field with the given tag and returns the record.
func (*Record) ReplaceField ¶
ReplaceField replaces the first field that shares f's tag with f, or appends f when no field has that tag. It returns the record.
func (*Record) SubfieldValue ¶
SubfieldValue returns the value of the first subfield with the given code in the first data field with the given tag, or the empty string.
func (*Record) SubfieldValues ¶
SubfieldValues returns the values of every subfield with the given code across all data fields with the given tag, in order.
func (*Record) Validate ¶
Validate reports the first structural problem with the record: a leader that is not 24 bytes, a field tag that is not 3 bytes, or a data field with no subfields. It returns nil when the record is structurally well-formed. It does not check tag semantics, indicator values, or character encoding.
type RecordReader ¶
RecordReader reads MARC records one at a time from an underlying stream. Each format subpackage provides an implementation; Read returns io.EOF when the stream is exhausted.
type RecordWriter ¶
RecordWriter serializes MARC records to an underlying stream. Each format subpackage provides an implementation.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bibframe converts MARC 21 records to BIBFRAME 2.0, the Library of Congress linked-data model that replaces the flat MARC record with an RDF graph of related resources: a bf:Work (the intellectual content) and a bf:Instance (a particular publication of it), linked by bf:instanceOf / bf:hasInstance.
|
Package bibframe converts MARC 21 records to BIBFRAME 2.0, the Library of Congress linked-data model that replaces the flat MARC record with an RDF graph of related resources: a bf:Work (the intellectual content) and a bf:Instance (a particular publication of it), linked by bf:instanceOf / bf:hasInstance. |
|
Package citation converts MARC 21 records to the RIS and BibTeX citation formats used by reference managers (Zotero, EndNote, Mendeley) and LaTeX.
|
Package citation converts MARC 21 records to the RIS and BibTeX citation formats used by reference managers (Zotero, EndNote, Mendeley) and LaTeX. |
|
Package dublincore converts MARC 21 records to Dublin Core (DCMI), the lowest-common-denominator metadata used by OAI-PMH and most repository software.
|
Package dublincore converts MARC 21 records to Dublin Core (DCMI), the lowest-common-denominator metadata used by OAI-PMH and most repository software. |
|
internal
|
|
|
marc8
Package marc8 decodes MARC-8 byte sequences to UTF-8 for the common Western subset: Basic Latin (ASCII, the default G0 set) and ANSEL Extended Latin (the default G1 set), including combining diacritics.
|
Package marc8 decodes MARC-8 byte sequences to UTF-8 for the common Western subset: Basic Latin (ASCII, the default G0 set) and ANSEL Extended Latin (the default G1 set), including combining diacritics. |
|
Package iso2709 reads and writes MARC 21 records in the binary ISO 2709 interchange format (.mrc), implementing codex.RecordReader and codex.RecordWriter.
|
Package iso2709 reads and writes MARC 21 records in the binary ISO 2709 interchange format (.mrc), implementing codex.RecordReader and codex.RecordWriter. |
|
Package marcjson reads and writes MARC 21 records in the de-facto "MARC-in-JSON" structure (the pymarc/ruby-marc/marc4j layout), implementing codex.RecordReader and codex.RecordWriter using only encoding/json.
|
Package marcjson reads and writes MARC 21 records in the de-facto "MARC-in-JSON" structure (the pymarc/ruby-marc/marc4j layout), implementing codex.RecordReader and codex.RecordWriter using only encoding/json. |
|
Package marcxml reads and writes MARC 21 records in the Library of Congress MARCXML "slim" serialization (namespace http://www.loc.gov/MARC21/slim), implementing codex.RecordReader and codex.RecordWriter using only encoding/xml.
|
Package marcxml reads and writes MARC 21 records in the Library of Congress MARCXML "slim" serialization (namespace http://www.loc.gov/MARC21/slim), implementing codex.RecordReader and codex.RecordWriter using only encoding/xml. |
|
Package mods converts MARC 21 records to MODS (Metadata Object Description Schema), the Library of Congress XML standard that is richer than MARCXML and near-lossless from MARC.
|
Package mods converts MARC 21 records to MODS (Metadata Object Description Schema), the Library of Congress XML standard that is richer than MARCXML and near-lossless from MARC. |
|
Package mrk reads and writes MARC 21 records in the MARCMaker / MARCBreaker mnemonic line format (".mrk"), implementing codex.RecordReader and codex.RecordWriter using only the standard library.
|
Package mrk reads and writes MARC 21 records in the MARCMaker / MARCBreaker mnemonic line format (".mrk"), implementing codex.RecordReader and codex.RecordWriter using only the standard library. |