hedi

package module
v0.0.0-...-a7a079c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2023 License: MIT Imports: 5 Imported by: 0

README

Hedi

Hedi is a library for interacting with Electronic Data Interchange (EDI) messages.

PkgGoDev Tests


Installation

go get github.com/pbjer/hedi

Usage

Lexing
msg := "ISA*01*0000000000*01*0000000000*ZZ*ABCDEFGHIJKLMNO*ZZ*123456789012345*101127*1719*U*00400*000003438*0*P*>~"
reader := strings.NewReader(msg)
lexer := hedi.NewLexer(reader)
tokens, err := lexer.Tokens()
if err != nil {
  // ...
}
Parsing
msg := "ISA*01*0000000000*01*0000000000*ZZ*ABCDEFGHIJKLMNO*ZZ*123456789012345*101127*1719*U*00400*000003438*0*P*>~"
reader := strings.NewReader(msg)
parser := hedi.NewParser(reader)
segments, err := parser.Segments()
if err != nil {
  // ...
}
Serialization
Stringer

Hedi's EDI types implement the String() string stringer interface for simple string serialization.

To override the default delimiters, you can use DString(d hedi.Delimiters) string, which Stringer depends on under the hood.

segments := hedi.Segments{{
  ID: "ST",
  Elements: hedi.Elements{{ Value: "850" }, { Value: "000000010" }},
}}

fmt.Println(segments)
// ST*850*000000010~

delimiters := hedi.Delimeters{
  Segment: '\n',
  Element: '|',
  SubElement: '>',
}
	
fmt.Println(segments.DString(delimiters))
// ST|850|000000010
//
WriterTo

Hedi's Segments EDI type implements the WriterTo interface for efficient string serialization to an io.Writer.

To override the default delimiters, you can use DWriteTo(d hedi.Delimiters, w io.Writer) (int64, error), which WriteTo depends on under the hood.

file, _ := os.Create("850.txt")
if err != nil {
  // ...
}
defer file.Close()

segments := hedi.Segments{{
  ID: "ST",
  Elements: hedi.Elements{{ Value: "850" }, { Value: "000000010" }},
}}

_, err = segments.WriteTo(file)
if err != nil {
  // ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSegmentIdentifierExpected is returned when a segment identifier is expected but not found.
	ErrSegmentIdentifierExpected = errors.New("segment identifier expected")
	// ErrElementExpected is returned when an element is expected but not found.
	ErrElementExpected = errors.New("element expected")
)
View Source
var DefaultDelimiters = Delimiters{
	Segment:    '~',
	Element:    '*',
	SubElement: '>',
}

DefaultDelimiters defines the default Delimiters used in EDI files.

View Source
var (
	// ErrInvalidISALength represents an error for invalid ISA segment length.
	ErrInvalidISALength = errors.New("invalid ISA length")
)

Functions

This section is empty.

Types

type Delimiters

type Delimiters struct {
	Segment    rune
	Element    rune
	SubElement rune
}

Delimiters contains the delimiters used for splitting segments, elements, and sub-elements in EDI files.

type Element

type Element struct {
	Value       string
	SubElements []string
}

Element represents an individual EDI element, containing a value and optional sub-elements.

func (*Element) AddSubElement

func (e *Element) AddSubElement(value string)

AddSubElement appends a sub-element value to the Element's SubElements slice. Initializes SubElements if it is nil.

func (Element) DString

func (e Element) DString(delimiters Delimiters) string

DString returns a delimited string representation of the Element. It formats the Element's value and sub-elements using the provided Delimiters.

func (Element) String

func (e Element) String() string

String returns the default delimited string representation of the Element. It uses the DefaultDelimiters for formatting.

type Elements

type Elements []Element

Elements is a slice of Element structs, often representing a list of elements in an EDI segment.

func (Elements) Last

func (ee Elements) Last() (*Element, bool)

Last returns the last Element in the Elements slice. Returns nil and false if the Elements slice is empty.

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer wraps an io.Reader for lexing EDI files.

func NewLexer

func NewLexer(reader io.Reader) *Lexer

NewLexer initializes a new Lexer with a given io.Reader.

func (*Lexer) Tokens

func (l *Lexer) Tokens() ([]Token, error)

Tokens lexes the input and returns a slice of Token structs. It expects an input that starts with a valid ISA segment of 106 bytes. Returns an error if the input does not meet the criteria.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser encapsulates the parsing logic for EDI files.

func NewParser

func NewParser(reader io.Reader) *Parser

NewParser creates a new Parser instance with the given io.Reader.

func (*Parser) Segments

func (p *Parser) Segments() (Segments, error)

Segments reads from the underlying reader and converts the token stream into Segments. It returns an error if the token stream does not conform to the expected structure.

type Segment

type Segment struct {
	ID       string
	Elements Elements
}

Segment represents an EDI segment, which consists of an ID and a list of Elements.

func NewSegment

func NewSegment(id string) *Segment

NewSegment constructs a new Segment with the given ID.

func (*Segment) AddElement

func (s *Segment) AddElement(element Element)

AddElement appends an Element to the end of the Segment.

func (Segment) DString

func (s Segment) DString(delimiters Delimiters) string

DString converts the Segment to its EDI string representation using the provided delimiters.

func (Segment) GetElement

func (s Segment) GetElement(index int) (Element, bool)

GetElement retrieves the Element at the specified index within the Segment. Returns the Element and a boolean indicating whether the Element was found.

func (*Segment) SetElement

func (s *Segment) SetElement(index int, element Element)

SetElement replaces or appends an Element at the specified index in the Segment. If the index exceeds the current size, the Elements slice is expanded.

func (Segment) String

func (s Segment) String() string

String converts the Segment to its EDI string representation using default delimiters.

type Segments

type Segments []Segment

Segments is a slice of Segment types.

func (*Segments) DString

func (s *Segments) DString(delimiters Delimiters) string

DString constructs a string representation of Segments using provided delimiters.

func (*Segments) DWriteTo

func (s *Segments) DWriteTo(d Delimiters, w io.Writer) (int64, error)

DWriteTo writes the Segments to an io.Writer w, formatted with specified delimiters. Returns the number of bytes written and any error encountered.

func (*Segments) Last

func (s *Segments) Last() (*Segment, bool)

Last returns a pointer to the last segment in the list, or nil if the list is empty. The boolean return value indicates the presence of a last segment.

func (Segments) String

func (s Segments) String() string

String satisfies the fmt.Stringer interface, delegating to DString.

func (*Segments) WriteTo

func (s *Segments) WriteTo(w io.Writer) (int64, error)

WriteTo satisfies the io.WriterTo interface, delegating to DWriteTo.

type Token

type Token struct {
	Type  TokenType `json:"type"`
	Value string    `json:"value"`
}

Token structure holding the type and value of a parsed token.

type TokenType

type TokenType string

TokenType represents the type of token in the parser.

const (
	// SegmentIdentifier represents the type of token that identifies a segment.
	SegmentIdentifier TokenType = "segment_identifier"

	// SegmentTerminator represents the type of token that terminates a segment.
	SegmentTerminator TokenType = "segment_terminator"

	// ElementValue represents the type of token that holds the value of an element.
	ElementValue TokenType = "element_value"

	// ElementDelimiter represents the type of token that delimits elements.
	ElementDelimiter TokenType = "element_delimiter"

	// SubElementValue represents the type of token that holds the value of a sub-element.
	SubElementValue TokenType = "sub_element_value"

	// SubElementDelimiter represents the type of token that delimits sub-elements.
	SubElementDelimiter TokenType = "sub_element_delimiter"
)

Enumerated TokenTypes for various elements in the segment structure.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL