MCPcopy Create free account
hub / github.com/benbjohnson/genesis / Encode

Method Encode

genesis.go:53–77  ·  view source on GitHub ↗

Encode encodes a single asset to the underlying writer.

(asset *Asset)

Source from the content-addressed store, hash-verified

51
52// Encode encodes a single asset to the underlying writer.
53func (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.
80func (enc *Encoder) Close() error {

Callers 2

TestEncoder_EncodeFunction · 0.95
runFunction · 0.95

Calls 3

ensureHeaderWrittenMethod · 0.95
hexFunction · 0.85
HashMethod · 0.80

Tested by 1

TestEncoder_EncodeFunction · 0.76