Encode encodes a single asset to the underlying writer.
(asset *Asset)
| 51 | |
| 52 | // Encode encodes a single asset to the underlying writer. |
| 53 | func (enc *Encoder) Encode(asset *Asset) error { |
| 54 | if err := enc.ensureHeaderWritten(); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | |
| 58 | // Ensure asset hasn't already been encoded. |
| 59 | if _, ok := enc.names[asset.Name]; ok { |
| 60 | return fmt.Errorf("duplicate asset: %q", asset.Name) |
| 61 | } |
| 62 | enc.names[asset.Name] = struct{}{} |
| 63 | |
| 64 | // Calculate mod time parts. |
| 65 | sec := asset.ModTime.UnixNano() / int64(time.Second) |
| 66 | nsec := asset.ModTime.UnixNano() % int64(time.Second) |
| 67 | |
| 68 | var buf bytes.Buffer |
| 69 | fmt.Fprintf(&buf, " %q: &File{\n", asset.Name) |
| 70 | fmt.Fprintf(&buf, " name: %q,\n", asset.Name) |
| 71 | fmt.Fprintf(&buf, " hash: %q,\n", asset.Hash()) |
| 72 | fmt.Fprintf(&buf, " modTime: time.Unix(%d, %d),\n", sec, nsec) |
| 73 | fmt.Fprintf(&buf, " data: []byte(\"%s\"),\n", string(hex(asset.Data))) |
| 74 | fmt.Fprintf(&buf, " },\n") |
| 75 | _, err := buf.WriteTo(enc.w) |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | // Close writes the rest of the file. |
| 80 | func (enc *Encoder) Close() error { |