gitlab.com/evatix-go/core@v1.3.55/codestack/Trace.go (about)

     1  package codestack
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"gitlab.com/evatix-go/core/constants"
     8  	"gitlab.com/evatix-go/core/coredata/corejson"
     9  	"gitlab.com/evatix-go/core/coredata/corestr"
    10  )
    11  
    12  type Trace struct {
    13  	SkipIndex int
    14  	PackageName,
    15  	MethodName,
    16  	PackageMethodName string
    17  	FilePath    string
    18  	Line        int
    19  	IsOkay      bool
    20  	message     corestr.SimpleStringOnce
    21  	shortString corestr.SimpleStringOnce
    22  }
    23  
    24  func (it *Trace) Message() string {
    25  	if it.message.IsInitialized() {
    26  		return it.message.String()
    27  	}
    28  
    29  	return it.
    30  		message.
    31  		GetPlusSetOnUninitializedFunc(
    32  			it.getCompiledMessage)
    33  }
    34  
    35  // ShortString
    36  //
    37  // Returns lazy or at once "Method (LineNumber) -> FileFullPath:LineNumber"
    38  // using format shortStringFormat "%s (%d) -> %s:%d"
    39  //
    40  // Format :
    41  //  - https://prnt.sc/25ypcyc : "%s (%d) -> %s:%d"
    42  //
    43  // Example :
    44  //  - "Method (LineNumber) -> FileFullPath:LineNumber"
    45  func (it *Trace) ShortString() string {
    46  	if it.shortString.IsInitialized() {
    47  		return it.shortString.String()
    48  	}
    49  
    50  	shortString := fmt.Sprintf(
    51  		shortStringFormat,
    52  		it.PackageMethodName,
    53  		it.Line,
    54  		it.FilePath,
    55  		it.Line)
    56  
    57  	return it.
    58  		shortString.
    59  		GetPlusSetOnUninitialized(
    60  			shortString)
    61  }
    62  
    63  func (it *Trace) IsNil() bool {
    64  	return it == nil
    65  }
    66  
    67  func (it *Trace) HasIssues() bool {
    68  	return it == nil || !it.IsOkay || it.PackageMethodName == "" || it.PackageName == ""
    69  }
    70  
    71  func (it *Trace) IsNotNil() bool {
    72  	return it != nil
    73  }
    74  
    75  func (it *Trace) String() string {
    76  	if it == nil {
    77  		return constants.EmptyString
    78  	}
    79  
    80  	return it.Message()
    81  }
    82  
    83  func (it Trace) StringUsingFmt(formatterFunc func(trace Trace) string) string {
    84  	return formatterFunc(it)
    85  }
    86  
    87  func (it *Trace) FileWithLine() FileWithLine {
    88  	return FileWithLine{
    89  		FilePath: it.FilePath,
    90  		Line:     it.Line,
    91  	}
    92  }
    93  
    94  // FullFilePath
    95  //
    96  // Returns the full file path
    97  func (it *Trace) FullFilePath() string {
    98  	return it.FilePath
    99  }
   100  
   101  // FileName
   102  //
   103  // Returns the file name only
   104  func (it *Trace) FileName() string {
   105  	return filepath.Base(it.FilePath)
   106  }
   107  
   108  func (it *Trace) LineNumber() int {
   109  	return it.Line
   110  }
   111  
   112  // FileWithLineString
   113  //
   114  // Format :
   115  //  - https://prnt.sc/25yorfh : "%s:%d"
   116  //
   117  // Example :
   118  //  - "FilePath:LineNumber"
   119  func (it *Trace) FileWithLineString() string {
   120  	return fmt.Sprintf(
   121  		fileWithLineFormat,
   122  		it.FilePath,
   123  		it.Line)
   124  }
   125  
   126  func (it *Trace) getCompiledMessage() string {
   127  	message := fmt.Sprintf(funcPrintFormat,
   128  		it.PackageMethodName,
   129  		it.Line,
   130  		it.FilePath,
   131  		it.Line)
   132  
   133  	return message
   134  }
   135  
   136  func (it Trace) JsonModel() Trace {
   137  	return it
   138  }
   139  
   140  func (it *Trace) JsonModelAny() interface{} {
   141  	return it.JsonModel()
   142  }
   143  
   144  func (it *Trace) Dispose() {
   145  	if it == nil {
   146  		return
   147  	}
   148  
   149  	it.SkipIndex = constants.Zero
   150  	it.PackageName = constants.EmptyString
   151  	it.MethodName = constants.EmptyString
   152  	it.PackageMethodName = constants.EmptyString
   153  	it.FilePath = constants.EmptyString
   154  	it.Line = constants.Zero
   155  	it.IsOkay = false
   156  	it.message.Dispose()
   157  	it.shortString.Dispose()
   158  }
   159  
   160  func (it *Trace) JsonString() string {
   161  	jsonResult := it.Json()
   162  
   163  	return jsonResult.JsonString()
   164  }
   165  
   166  func (it Trace) Json() corejson.Result {
   167  	return corejson.New(it)
   168  }
   169  
   170  func (it Trace) JsonPtr() *corejson.Result {
   171  	return corejson.NewPtr(it)
   172  }
   173  
   174  func (it *Trace) ParseInjectUsingJson(
   175  	jsonResult *corejson.Result,
   176  ) (*Trace, error) {
   177  	err := jsonResult.Unmarshal(it)
   178  
   179  	if err != nil {
   180  		return nil, err
   181  	}
   182  
   183  	return it, nil
   184  }
   185  
   186  // ParseInjectUsingJsonMust Panic if error
   187  //goland:noinspection GoLinterLocal
   188  func (it *Trace) ParseInjectUsingJsonMust(
   189  	jsonResult *corejson.Result,
   190  ) *Trace {
   191  	newUsingJson, err :=
   192  		it.ParseInjectUsingJson(jsonResult)
   193  
   194  	if err != nil {
   195  		panic(err)
   196  	}
   197  
   198  	return newUsingJson
   199  }
   200  
   201  func (it *Trace) JsonParseSelfInject(
   202  	jsonResult *corejson.Result,
   203  ) error {
   204  	_, err := it.ParseInjectUsingJson(
   205  		jsonResult,
   206  	)
   207  
   208  	return err
   209  }
   210  
   211  func (it Trace) Clone() Trace {
   212  	return Trace{
   213  		SkipIndex:         it.SkipIndex,
   214  		PackageName:       it.PackageName,
   215  		MethodName:        it.MethodName,
   216  		PackageMethodName: it.PackageMethodName,
   217  		FilePath:          it.FilePath,
   218  		Line:              it.Line,
   219  		IsOkay:            it.IsOkay,
   220  	}
   221  }
   222  
   223  func (it *Trace) ClonePtr() *Trace {
   224  	if it == nil {
   225  		return nil
   226  	}
   227  
   228  	trace := it.Clone()
   229  
   230  	return &trace
   231  }
   232  
   233  func (it *Trace) AsFileLiner() FileWithLiner {
   234  	return it
   235  }