inline.go 731 Bytes
Newer Older
Kevin Atkinson's avatar
Kevin Atkinson committed
1 2 3 4
package cidutil

import (
	mhash "github.com/multiformats/go-multihash"
5
	cid "gitlab.dms3.io/dms3/go-cid"
Kevin Atkinson's avatar
Kevin Atkinson committed
6 7
)

8
// InlineBuilder is a cid.Builder that will use the id multihash when the
Kevin Atkinson's avatar
Kevin Atkinson committed
9
// size of the content is no more than limit
10 11 12
type InlineBuilder struct {
	cid.Builder     // Parent Builder
	Limit       int // Limit (inclusive)
Kevin Atkinson's avatar
Kevin Atkinson committed
13 14 15
}

// WithCodec implements the cid.Builder interface
16 17
func (p InlineBuilder) WithCodec(c uint64) cid.Builder {
	return InlineBuilder{p.Builder.WithCodec(c), p.Limit}
Kevin Atkinson's avatar
Kevin Atkinson committed
18 19 20
}

// Sum implements the cid.Builder interface
21
func (p InlineBuilder) Sum(data []byte) (cid.Cid, error) {
Kevin Atkinson's avatar
Kevin Atkinson committed
22 23 24 25 26
	if len(data) > p.Limit {
		return p.Builder.Sum(data)
	}
	return cid.V1Builder{Codec: p.GetCodec(), MhType: mhash.ID}.Sum(data)
}