github.com/enetx/g@v1.0.80/file_encdec.go (about)

     1  package g
     2  
     3  import (
     4  	"encoding/gob"
     5  	"encoding/json"
     6  )
     7  
     8  type (
     9  	// fenc represents a wrapper for file encoding.
    10  	fenc struct{ f *File }
    11  
    12  	// fdec represents a wrapper for file decoding.
    13  	fdec struct{ f *File }
    14  )
    15  
    16  // Enc returns an fenc struct wrapping the given file for encoding.
    17  func (f *File) Enc() fenc { return fenc{f} }
    18  
    19  // Dec returns an fdec struct wrapping the given file for decoding.
    20  func (f *File) Dec() fdec { return fdec{f} }
    21  
    22  // Gob encodes the provided data using the encoding/gob package and writes it to the file.
    23  // It returns a Result[*File] indicating the success or failure of the encoding operation.
    24  //
    25  // If the encoding operation is successful, the created file is closed automatically.
    26  //
    27  // Usage:
    28  //
    29  //	data := g.SliceOf(1, 2, 3, 4)
    30  //	result := g.NewFile("somefile.gob").Enc().Gob(data)
    31  //
    32  // Parameters:
    33  //   - data: The data to be encoded and written to the file.
    34  //
    35  // Returns:
    36  //   - Result[*File]: A Result containing a *File if the operation is successful; otherwise, an error Result.
    37  func (fe fenc) Gob(data any) Result[*File] {
    38  	r := fe.f.Create()
    39  	if r.IsErr() {
    40  		return r
    41  	}
    42  
    43  	defer r.Ok().Close()
    44  
    45  	if err := gob.NewEncoder(r.Ok().Std()).Encode(data); err != nil {
    46  		return Err[*File](err)
    47  	}
    48  
    49  	return r
    50  }
    51  
    52  // Gob decodes data from the file using the encoding/gob package and populates the provided data structure.
    53  // It returns a Result[*File] indicating the success or failure of the decoding operation.
    54  //
    55  // If the decoding operation is successful, the file is closed automatically.
    56  //
    57  // Usage:
    58  //
    59  //	var data g.Slice[int]
    60  //	result := g.NewFile("somefile.gob").Dec().Gob(&data)
    61  //
    62  // Parameters:
    63  //   - data: A pointer to the data structure where the decoded data will be stored.
    64  //
    65  // Returns:
    66  //   - Result[*File]: A Result containing a *File if the operation is successful; otherwise, an error Result.
    67  func (fd fdec) Gob(data any) Result[*File] {
    68  	r := fd.f.Open()
    69  	if r.IsErr() {
    70  		return r
    71  	}
    72  
    73  	defer r.Ok().Close()
    74  
    75  	if err := gob.NewDecoder(r.Ok().Std()).Decode(data); err != nil {
    76  		return Err[*File](err)
    77  	}
    78  
    79  	return r
    80  }
    81  
    82  // JSON encodes the provided data using the encoding/json package and writes it to the file.
    83  // It returns a Result[*File] indicating the success or failure of the encoding operation.
    84  //
    85  // If the encoding operation is successful, the created file is closed automatically.
    86  //
    87  // Usage:
    88  //
    89  //	data := g.SliceOf(1, 2, 3, 4)
    90  //	result := g.NewFile("somefile.json").Enc().JSON(data)
    91  //
    92  // Parameters:
    93  //   - data: The data to be encoded and written to the file.
    94  //
    95  // Returns:
    96  //   - Result[*File]: A Result containing a *File if the operation is successful; otherwise, an error Result.
    97  func (fe fenc) JSON(data any) Result[*File] {
    98  	r := fe.f.Create()
    99  	if r.IsErr() {
   100  		return r
   101  	}
   102  
   103  	defer r.Ok().Close()
   104  
   105  	if err := json.NewEncoder(r.Ok().Std()).Encode(data); err != nil {
   106  		return Err[*File](err)
   107  	}
   108  
   109  	return r
   110  }
   111  
   112  // JSON decodes data from the file using the encoding/json package and populates the provided data structure.
   113  // It returns a Result[*File] indicating the success or failure of the decoding operation.
   114  //
   115  // If the decoding operation is successful, the file is closed automatically.
   116  //
   117  // Usage:
   118  //
   119  //	var data g.Slice[int]
   120  //	result := g.NewFile("somefile.json").Dec().JSON(&data)
   121  //
   122  // Parameters:
   123  //   - data: A pointer to the data structure where the decoded data will be stored.
   124  //
   125  // Returns:
   126  //   - Result[*File]: A Result containing a *File if the operation is successful; otherwise, an error Result.
   127  func (fd fdec) JSON(data any) Result[*File] {
   128  	r := fd.f.Open()
   129  	if r.IsErr() {
   130  		return r
   131  	}
   132  
   133  	defer r.Ok().Close()
   134  
   135  	if err := json.NewDecoder(r.Ok().Std()).Decode(data); err != nil {
   136  		return Err[*File](err)
   137  	}
   138  
   139  	return r
   140  }