Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write DCC encoder implementation #2

Open
gravestench opened this issue Jul 1, 2021 · 0 comments
Open

Write DCC encoder implementation #2

gravestench opened this issue Jul 1, 2021 · 0 comments

Comments

@gravestench
Copy link
Contributor

gravestench commented Jul 1, 2021

We should write a DCC encoder as a method of the DCC struct.

func (d *DCC) Encode(w io.Writer) error { /* ... */ }

This should fully marshal our DCC struct to a byte slice, by writing the bytes sequentially into the given stream.

The use-case, as a user of this library, looks something like this:

import (
    "bufio"
    "ioutil"
    "os"

    dcc "github.com/gravestench/dcc/pkg"
)

func main() {
    // load a dcc file
    fileContents, err := ioutil.ReadFile("/path/to/something.dcc")
    if err != nil {
        // file io error
    }

    // parse the dcc file data into a dcc struct
    instance, err := dcc.FromBytes(fileContents)
    if err != nil {
        // dcc parse error
    }

    // re-encode the struct back to dcc data, should be identical to original file data
    f, err := os.Create("/path/to/something_else.dcc")
    if err != nil {
        // file io error
    }

    defer f.Close()

    stream := bufio.NewWriter(w)
    if err := instance.Encode(stream); err != nil {
        // dcc encode error
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant