Parse parses a patch with changes to one or more files. Any content before the first file is returned as the second value. If an error occurs while parsing, it returns all files parsed before the error. Parse expects to receive a single patch. If the input may contain multiple patches (for example,
(r io.Reader)
| 17 | // patches (for example, if it is an mbox file), callers should split it into |
| 18 | // individual patches and call Parse on each one. |
| 19 | func Parse(r io.Reader) ([]*File, string, error) { |
| 20 | p := newParser(r) |
| 21 | |
| 22 | if err := p.Next(); err != nil { |
| 23 | if err == io.EOF { |
| 24 | return nil, "", nil |
| 25 | } |
| 26 | return nil, "", err |
| 27 | } |
| 28 | |
| 29 | var preamble string |
| 30 | var files []*File |
| 31 | for { |
| 32 | file, pre, err := p.ParseNextFileHeader() |
| 33 | if err != nil { |
| 34 | return files, preamble, err |
| 35 | } |
| 36 | if len(files) == 0 { |
| 37 | preamble = pre |
| 38 | } |
| 39 | if file == nil { |
| 40 | break |
| 41 | } |
| 42 | |
| 43 | for _, fn := range []func(*File) (int, error){ |
| 44 | p.ParseTextFragments, |
| 45 | p.ParseBinaryFragments, |
| 46 | } { |
| 47 | n, err := fn(file) |
| 48 | if err != nil { |
| 49 | return files, preamble, err |
| 50 | } |
| 51 | if n > 0 { |
| 52 | break |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | files = append(files, file) |
| 57 | } |
| 58 | |
| 59 | return files, preamble, nil |
| 60 | } |
| 61 | |
| 62 | // TODO(bkeyes): consider exporting the parser type with configuration |
| 63 | // this would enable OID validation, p-value guessing, and prefix stripping |