Parse decodes lockfile JSON lines. Non-empty files must start with a version header line: [["version","1"]].
(data []byte)
| 52 | // |
| 53 | // Non-empty files must start with a version header line: [["version","1"]]. |
| 54 | func Parse(data []byte) (*Lockfile, error) { |
| 55 | lock := New() |
| 56 | lines := bytes.Split(data, []byte("\n")) |
| 57 | |
| 58 | firstContentLine := true |
| 59 | for i, rawLine := range lines { |
| 60 | line := strings.TrimSpace(string(rawLine)) |
| 61 | if line == "" { |
| 62 | continue |
| 63 | } |
| 64 | |
| 65 | if firstContentLine { |
| 66 | if err := parseVersionHeader([]byte(line)); err != nil { |
| 67 | return nil, fmt.Errorf("lockfile line %d: %w", i+1, err) |
| 68 | } |
| 69 | firstContentLine = false |
| 70 | continue |
| 71 | } |
| 72 | |
| 73 | entry, err := parseEntry([]byte(line)) |
| 74 | if err != nil { |
| 75 | return nil, fmt.Errorf("lockfile line %d: %w", i+1, err) |
| 76 | } |
| 77 | lock.entries[entryKey(entry.namespace, entry.operation, entry.inputsJSON)] = entry |
| 78 | } |
| 79 | |
| 80 | return lock, nil |
| 81 | } |
| 82 | |
| 83 | // Marshal encodes lockfile entries to deterministic JSON lines. |
| 84 | // |