/go-pic

COBOL PIC clause parsing & struct gen

Primary LanguageGoMIT LicenseMIT

📺 go-pic

COBOL PIC (Picture) clause parsing library

Go Report Card GoDoc Sourcegraph Release License codebeat badge

🤷🏽‍ What is go-pic

gopic is both a tool and package, it's purpose is to help you process COBOL copybook files with ease

📤 Unmarshaller

gopic can be used to enable simpler 1:1 mapping of PIC definitions to Go structs.

Simply open your data file and pass it to a gopic decoder with the struct that represents the source copybook

Show usage
  1. Import gopic

    import (
        pic "github.com/foundatn-io/go-pic"
    )
  2. Tag your structs

    Say your copybook data looks like

    000180      15  PropertyA    PIC X(5).     00000117
    000190      15  PropertyB    PIC X(2).     00000118
    

    You would tag your struct like so

    type yourStruct struct {
        PropertyA string `pic:"5"` 
        PropertyB string `pic:"2"`
    }
  3. Prepare a decoder and unmarshal your input

    d := pic.NewDecoder(f) // where f is your io.Reader / data
    typ := yourStruct{} // with pic tags
    if err := d.Decode(typ); err != nil {
            log.Fatal(err)
    }

📥 Struct generator

gopic can be used to generate simpler 1:1 mapping of PIC definitions to Go structs.

gopic provides support to generate flattened Go struct representations of your COBOL copybooks, tagged with length statements and assigned the appropriate type for unmarshalling files that match your COBOL copybook definitions, so you need not manually create or tag your structs when using go-pic for unmarshalling.

Show usage
  1. Install gopic!

    Get started using gopic by for struct generation by running:

    git clone github.com/foundatn-io/go-pic
    cd go-pic
    make install
  2. Generate structs from a copybook file (long-form flags)

    gopic file --package=shipping --output=shipping --input=cobolstuff/copybook-shipping.txt
  3. Generate many structs from a directory containing only copybooks (short-form flags)

    gopic dir -p mystructsdir -o mystructsdir -i cobolstuff

When using gopic for struct generation, additional, non-functional values are tagged to the PIC tags, for legibility's sake.

For example, the example copybook clauses:

000180      15  PropertyA    PIC 9(5).     00000117
000190      15  PropertyB    PIC X(2).     00000118

if generated with gopic becomes:

type Copybook struct{
    PropertyA uint      `pic:"9"`  // start:1 end:9
    PropertyB string    `pic:"2"`  // start:10 end:11
}

🚧 Alas, these are not yet supported

  • PIC symbols [ S, V, P ]
  • Level indicator 88 enums are skipped
  • Level indicator 77 items which cannot be sub-divided