gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/serializerLogic.go (about)

     1  package corejson
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"gitlab.com/evatix-go/core/errcore"
     8  	"gitlab.com/evatix-go/core/internal/reflectinternal"
     9  )
    10  
    11  type serializerLogic struct{}
    12  
    13  func (it serializerLogic) StringsApply(
    14  	slice []string,
    15  ) *Result {
    16  	return it.Apply(slice)
    17  }
    18  
    19  func (it serializerLogic) Apply(
    20  	anyItem interface{},
    21  ) *Result {
    22  	jsonBytes, err := json.Marshal(
    23  		anyItem)
    24  	typeName := reflectinternal.TypeName(
    25  		anyItem)
    26  
    27  	if err != nil {
    28  		return &Result{
    29  			Bytes: jsonBytes,
    30  			Error: errcore.
    31  				MarshallingFailedType.Error(
    32  				err.Error(),
    33  				typeName),
    34  			TypeName: typeName,
    35  		}
    36  	}
    37  
    38  	return &Result{
    39  		Bytes:    jsonBytes,
    40  		Error:    err,
    41  		TypeName: typeName,
    42  	}
    43  }
    44  
    45  func (it serializerLogic) FromBytes(
    46  	currentBytes []byte,
    47  ) *Result {
    48  	return it.Apply(currentBytes)
    49  }
    50  
    51  func (it serializerLogic) FromStrings(
    52  	lines []string,
    53  ) *Result {
    54  	return it.Apply(lines)
    55  }
    56  
    57  func (it serializerLogic) FromStringsSpread(
    58  	lines ...string,
    59  ) *Result {
    60  	return it.Apply(lines)
    61  }
    62  
    63  func (it serializerLogic) FromString(
    64  	line string,
    65  ) *Result {
    66  	return it.Apply(line)
    67  }
    68  
    69  func (it serializerLogic) FromInteger(
    70  	integer int,
    71  ) *Result {
    72  	return it.Apply(integer)
    73  }
    74  
    75  func (it serializerLogic) FromInteger64(
    76  	integer64 int,
    77  ) *Result {
    78  	return it.Apply(integer64)
    79  }
    80  
    81  func (it serializerLogic) FromBool(
    82  	isResult bool,
    83  ) *Result {
    84  	return it.Apply(isResult)
    85  }
    86  
    87  func (it serializerLogic) FromIntegers(
    88  	integers []int,
    89  ) *Result {
    90  	return it.Apply(integers)
    91  }
    92  
    93  func (it serializerLogic) FromStringer(
    94  	stringer fmt.Stringer,
    95  ) *Result {
    96  	return it.Apply(stringer.String())
    97  }
    98  
    99  func (it serializerLogic) UsingAnyPtr(
   100  	anyItem interface{},
   101  ) *Result {
   102  	jsonBytes, err := json.Marshal(
   103  		anyItem)
   104  	typeName := reflectinternal.TypeName(
   105  		anyItem)
   106  
   107  	if err != nil {
   108  		finalErr := errcore.
   109  			MarshallingFailedType.Error(
   110  			err.Error(),
   111  			typeName)
   112  
   113  		return &Result{
   114  			Bytes:    jsonBytes,
   115  			Error:    finalErr,
   116  			TypeName: typeName,
   117  		}
   118  	}
   119  
   120  	return &Result{
   121  		Bytes:    jsonBytes,
   122  		Error:    err,
   123  		TypeName: typeName,
   124  	}
   125  }
   126  
   127  func (it serializerLogic) UsingAny(
   128  	anyItem interface{},
   129  ) Result {
   130  	return it.Apply(anyItem).NonPtr()
   131  }
   132  
   133  func (it serializerLogic) Raw(
   134  	anyItem interface{},
   135  ) ([]byte, error) {
   136  	jsonResult := it.Apply(anyItem)
   137  
   138  	return jsonResult.Raw()
   139  }
   140  
   141  func (it serializerLogic) Marshal(
   142  	anyItem interface{},
   143  ) ([]byte, error) {
   144  	jsonResult := it.Apply(anyItem)
   145  
   146  	return jsonResult.Raw()
   147  }
   148  
   149  func (it serializerLogic) ApplyMust(
   150  	anyItem interface{},
   151  ) *Result {
   152  	result := it.Apply(anyItem)
   153  	result.MustBeSafe()
   154  
   155  	return result
   156  }
   157  
   158  func (it serializerLogic) ToBytesMust(
   159  	anyItem interface{},
   160  ) []byte {
   161  	result := it.Apply(anyItem)
   162  	result.MustBeSafe()
   163  
   164  	return result.Bytes
   165  }
   166  
   167  func (it serializerLogic) ToSafeBytesMust(
   168  	anyItem interface{},
   169  ) []byte {
   170  	result := it.Apply(anyItem)
   171  	result.MustBeSafe()
   172  
   173  	return result.SafeBytes()
   174  }
   175  
   176  // ToSafeBytesSwallowErr
   177  //
   178  // Warning or Danger:
   179  //  - shallow err by not throwing or returning (could be dangerous as well)
   180  //
   181  // Notes :
   182  //  - To inform use Err or Apply or must methods
   183  //
   184  // Use case (rarely):
   185  //  - When don't care about the error just proceed with the value.
   186  func (it serializerLogic) ToSafeBytesSwallowErr(
   187  	anyItem interface{},
   188  ) []byte {
   189  	result := it.Apply(anyItem)
   190  
   191  	return result.SafeBytes()
   192  }
   193  
   194  // ToBytesSwallowErr
   195  //
   196  // Warning or Danger:
   197  //  - shallow err by not throwing or returning (could be dangerous as well)
   198  //
   199  // Notes :
   200  //  - To inform use Err or Apply or must methods
   201  //
   202  // Use case (rarely):
   203  //  - When don't care about the error just proceed with the value.
   204  func (it serializerLogic) ToBytesSwallowErr(
   205  	anyItem interface{},
   206  ) []byte {
   207  	result := it.Apply(anyItem)
   208  
   209  	return result.Bytes
   210  }
   211  
   212  func (it serializerLogic) ToBytesErr(
   213  	anyItem interface{},
   214  ) ([]byte, error) {
   215  	result := it.Apply(anyItem)
   216  
   217  	return result.Bytes, result.MeaningfulError()
   218  }
   219  
   220  // ToString
   221  //
   222  // Warning:
   223  //  - Shallow err by not throwing or
   224  //      returning (could be dangerous as well)
   225  //  - However, with this version
   226  //      if error occurred then error will be returned as string.
   227  //
   228  // Notes :
   229  //  - To inform use Err or Apply or must methods
   230  //
   231  // Use case (rarely):
   232  //  - When don't care about the error just proceed with the value.
   233  func (it serializerLogic) ToString(
   234  	anyItem interface{},
   235  ) string {
   236  	result := it.Apply(anyItem)
   237  
   238  	return result.JsonString()
   239  }
   240  
   241  func (it serializerLogic) ToStringMust(
   242  	anyItem interface{},
   243  ) string {
   244  	result := it.Apply(anyItem)
   245  	result.HandleError()
   246  
   247  	return result.JsonString()
   248  }
   249  
   250  func (it serializerLogic) ToStringErr(
   251  	anyItem interface{},
   252  ) (string, error) {
   253  	result := it.Apply(anyItem)
   254  
   255  	return result.RawString()
   256  }
   257  
   258  func (it serializerLogic) ToPrettyStringErr(
   259  	anyItem interface{},
   260  ) (string, error) {
   261  	result := it.Apply(anyItem)
   262  
   263  	return result.RawPrettyString()
   264  }
   265  
   266  // ToPrettyStringIncludingErr
   267  //
   268  // Warning:
   269  //  - Shallow err by not throwing or
   270  //      returning (could be dangerous as well)
   271  //  - However, with this version
   272  //      if error occurred then error will be returned as string.
   273  //
   274  // Notes :
   275  //  - To inform use Err or Apply or must methods
   276  //
   277  // Use case (rarely):
   278  //  - When don't care about the error just proceed with the value.
   279  func (it serializerLogic) ToPrettyStringIncludingErr(
   280  	anyItem interface{},
   281  ) string {
   282  	result := it.Apply(anyItem)
   283  
   284  	return result.PrettyJsonStringOrErrString()
   285  }