Skip to content

Commit

Permalink
Merge pull request #12 from mazrean/doc/godoc
Browse files Browse the repository at this point in the history
write godoc
  • Loading branch information
mazrean authored Mar 6, 2024
2 parents 3216ba1 + 9fab9ee commit f4f314b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
18 changes: 18 additions & 0 deletions formstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,32 @@ const (
defaultMaxMemFileSize = 32 * MB
)

// WithMaxParts sets the maximum number of parts to be parsed.
// default: 10000
func WithMaxParts(maxParts uint) ParserOption {
return func(c *parserConfig) {
c.maxParts = maxParts
}
}

// WithMaxHeaders sets the maximum number of headers to be parsed.
// default: 10000
func WithMaxHeaders(maxHeaders uint) ParserOption {
return func(c *parserConfig) {
c.maxHeaders = maxHeaders
}
}

// WithMaxMemSize sets the maximum memory size to be used for parsing.
// default: 32MB
func WithMaxMemSize(maxMemSize DataSize) ParserOption {
return func(c *parserConfig) {
c.maxMemSize = maxMemSize
}
}

// WithMaxMemFileSize sets the maximum memory size to be used for parsing a file.
// default: 32MB
func WithMaxMemFileSize(maxMemFileSize DataSize) ParserOption {
return func(c *parserConfig) {
c.maxMemFileSize = maxMemFileSize
Expand All @@ -86,10 +94,12 @@ type Value struct {
header Header
}

// Unwrap returns the content and header of the value.
func (v Value) Unwrap() (string, Header) {
return string(v.content), v.header
}

// UnwrapRaw returns the raw content and header of the value.
func (v Value) UnwrapRaw() ([]byte, Header) {
return v.content, v.header
}
Expand All @@ -112,18 +122,26 @@ func newHeader(h textproto.MIMEHeader) Header {
}
}

// Get returns the first value associated with the given key.
// If there are no values associated with the key, Get returns "".
func (h Header) Get(key string) string {
return h.header.Get(key)
}

// ContentType returns the value of the "Content-Type" header field.
// If there are no values associated with the key, ContentType returns "".
func (h Header) ContentType() string {
return h.header.Get("Content-Type")
}

// Name returns the value of the "name" parameter in the "Content-Disposition" header field.
// If there are no values associated with the key, Name returns "".
func (h Header) Name() string {
return h.dispositionParams["name"]
}

// FileName returns the value of the "filename" parameter in the "Content-Disposition" header field.
// If there are no values associated with the key, FileName returns "".
func (h Header) FileName() string {
return h.dispositionParams["filename"]
}
Expand Down
4 changes: 3 additions & 1 deletion formstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ func createSampleForm(w io.Writer, fileSize formstream.DataSize, boundary string
if err != nil {
return fmt.Errorf("failed to create part: %w", err)
}

mbData := make([]byte, formstream.MB)
for i := 0; i < int(fileSize/formstream.MB); i++ {
_, err := pw.Write([]byte(strings.Repeat("a", int(formstream.MB))))
_, err := pw.Write(mbData)
if err != nil {
return fmt.Errorf("failed to write: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
ErrTooLargeForm = errors.New("too large form")
)

// Parse parses the multipart form from r.
func (p *Parser) Parse(r io.Reader) (err error) {
hsc := newHookSatisfactionChecker(p.hookMap, &p.parserConfig)
defer func() {
Expand Down
2 changes: 2 additions & 0 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
)

// Register registers a stream hook with the given name.
func (p *Parser) Register(name string, fn StreamHookFunc, options ...RegisterOption) error {
if _, ok := p.hookMap[name]; ok {
return DuplicateHookNameError{Name: name}
Expand Down Expand Up @@ -36,6 +37,7 @@ type registerConfig struct {

type RegisterOption func(*registerConfig)

// WithRequiredPart sets the required part names for the stream hook.
func WithRequiredPart(name string) RegisterOption {
return func(c *registerConfig) {
c.requireParts = append(c.requireParts, name)
Expand Down

0 comments on commit f4f314b

Please sign in to comment.