gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/newSimpleSliceCreator.go (about)

     1  package corestr
     2  
     3  import (
     4  	"strings"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  	"gitlab.com/evatix-go/core/coredata/corejson"
     8  )
     9  
    10  type newSimpleSliceCreator struct{}
    11  
    12  func (it *newSimpleSliceCreator) Cap(capacity int) *SimpleSlice {
    13  	slice := make([]string, 0, capacity)
    14  
    15  	return &SimpleSlice{
    16  		slice,
    17  	}
    18  }
    19  
    20  // Default
    21  //
    22  //  Capacity 10
    23  func (it *newSimpleSliceCreator) Default() *SimpleSlice {
    24  	slice := make([]string, 0, constants.Capacity10)
    25  
    26  	return &SimpleSlice{
    27  		slice,
    28  	}
    29  }
    30  
    31  func (it *newSimpleSliceCreator) DefaultSlice() SimpleSlice {
    32  	slice := make([]string, 0, constants.Capacity5)
    33  
    34  	return SimpleSlice{
    35  		slice,
    36  	}
    37  }
    38  
    39  func (it *newSimpleSliceCreator) Deserialize(
    40  	jsonBytes []byte,
    41  ) (*SimpleSlice, error) {
    42  	lines, err := corejson.Deserialize.BytesTo.Strings(jsonBytes)
    43  
    44  	if err == nil {
    45  		return it.Strings(lines), nil
    46  	}
    47  
    48  	return it.Empty(), err
    49  }
    50  
    51  func (it *newSimpleSliceCreator) DeserializeJsoner(
    52  	jsoner corejson.Jsoner,
    53  ) (*SimpleSlice, error) {
    54  	empty := it.Empty()
    55  
    56  	err := corejson.
    57  		Deserialize.
    58  		UsingJsonerToAny(
    59  			true,
    60  			jsoner,
    61  			empty)
    62  
    63  	if err == nil {
    64  		return empty, nil
    65  	}
    66  
    67  	return empty, err
    68  }
    69  
    70  func (it *newSimpleSliceCreator) UsingLines(
    71  	isClone bool,
    72  	lines ...string,
    73  ) *SimpleSlice {
    74  	if lines == nil {
    75  		return it.Empty()
    76  	}
    77  
    78  	if !isClone {
    79  		return &SimpleSlice{
    80  			lines,
    81  		}
    82  	}
    83  
    84  	slice := it.Cap(len(lines))
    85  
    86  	return slice.Adds(lines...)
    87  }
    88  
    89  // Lines
    90  //
    91  //  don't clone
    92  func (it *newSimpleSliceCreator) Lines(
    93  	lines ...string,
    94  ) *SimpleSlice {
    95  	return &SimpleSlice{
    96  		lines,
    97  	}
    98  }
    99  
   100  func (it *newSimpleSliceCreator) SpreadStrings(
   101  	lines ...string,
   102  ) *SimpleSlice {
   103  	return &SimpleSlice{
   104  		lines,
   105  	}
   106  }
   107  
   108  func (it *newSimpleSliceCreator) Create(
   109  	lines []string,
   110  ) *SimpleSlice {
   111  	return &SimpleSlice{
   112  		lines,
   113  	}
   114  }
   115  
   116  func (it *newSimpleSliceCreator) Strings(
   117  	lines []string,
   118  ) *SimpleSlice {
   119  	return &SimpleSlice{
   120  		lines,
   121  	}
   122  }
   123  
   124  func (it *newSimpleSliceCreator) StringsPtr(
   125  	lines *[]string,
   126  ) *SimpleSlice {
   127  	if lines == nil || len(*lines) == 0 {
   128  		return it.Empty()
   129  	}
   130  
   131  	return &SimpleSlice{
   132  		*lines,
   133  	}
   134  }
   135  
   136  func (it *newSimpleSliceCreator) StringsPtrOption(
   137  	isClone bool,
   138  	lines *[]string,
   139  ) *SimpleSlice {
   140  	if lines == nil || len(*lines) == 0 {
   141  		return it.Empty()
   142  	}
   143  
   144  	if !isClone {
   145  		return &SimpleSlice{
   146  			*lines,
   147  		}
   148  	}
   149  
   150  	return it.StringsClone(*lines)
   151  }
   152  
   153  func (it *newSimpleSliceCreator) StringsClone(
   154  	lines []string,
   155  ) *SimpleSlice {
   156  	if lines == nil {
   157  		return it.Empty()
   158  	}
   159  
   160  	slice := it.Cap(len(lines))
   161  
   162  	return slice.Adds(lines...)
   163  }
   164  
   165  func (it *newSimpleSliceCreator) Direct(
   166  	isClone bool,
   167  	lines []string,
   168  ) *SimpleSlice {
   169  	if lines == nil {
   170  		return it.Empty()
   171  	}
   172  
   173  	if !isClone {
   174  		return &SimpleSlice{
   175  			lines,
   176  		}
   177  	}
   178  
   179  	slice := it.Cap(len(lines))
   180  
   181  	return slice.Adds(lines...)
   182  }
   183  
   184  func (it *newSimpleSliceCreator) UsingSeparatorLine(
   185  	sep, line string,
   186  ) *SimpleSlice {
   187  	lines := strings.Split(line, sep)
   188  
   189  	return &SimpleSlice{
   190  		Items: lines,
   191  	}
   192  }
   193  
   194  func (it *newSimpleSliceCreator) UsingLine(
   195  	combinedLine string,
   196  ) *SimpleSlice {
   197  	lines := strings.Split(combinedLine, constants.DefaultLine)
   198  
   199  	return &SimpleSlice{
   200  		Items: lines,
   201  	}
   202  }
   203  
   204  func (it *newSimpleSliceCreator) Empty() *SimpleSlice {
   205  	return &SimpleSlice{
   206  		nil,
   207  	}
   208  }