Skip to content

Commit

Permalink
added NewMemFileWriteJSON, NewMemFileWriteXML
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed May 16, 2024
1 parent 7cb7200 commit c37df24
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ func NewMemFile(name string, data []byte) MemFile {
return MemFile{FileName: name, FileData: data}
}

// NewMemFileWriteJSON returns a new MemFile with the input mashalled to JSON as FileData.
// Any indent arguments will be concanated and used as JSON line indentation.
func NewMemFileWriteJSON(name string, input any, indent ...string) (MemFile, error) {
var (
data []byte
err error
)
if len(indent) == 0 {
data, err = json.Marshal(input)
} else {
data, err = json.MarshalIndent(input, "", strings.Join(indent, ""))
}
if err != nil {
return MemFile{}, err
}
return MemFile{FileName: name, FileData: data}, nil
}

// NewMemFileWriteXML returns a new MemFile with the input mashalled to XML as FileData.
// Any indent arguments will be concanated and used as XML line indentation.
func NewMemFileWriteXML(name string, input any, indent ...string) (MemFile, error) {
var (
data []byte
err error
)
if len(indent) == 0 {
data, err = xml.Marshal(input)
} else {
data, err = xml.MarshalIndent(input, "", strings.Join(indent, ""))
}
if err != nil {
return MemFile{}, err
}
return MemFile{FileName: name, FileData: append([]byte(xml.Header), data...)}, nil
}

// ReadMemFile returns a new MemFile with name and data from fileReader.
// If the passed fileReader is a MemFile then
// its FileData is used directly without copying it.
Expand Down

0 comments on commit c37df24

Please sign in to comment.