gitlab.com/evatix-go/core@v1.3.55/coretaskinfo/Info.go (about)

     1  package coretaskinfo
     2  
     3  import (
     4  	"strings"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  	"gitlab.com/evatix-go/core/coredata/corejson"
     8  	"gitlab.com/evatix-go/core/coredata/corestr"
     9  )
    10  
    11  type Info struct {
    12  	RootName       string            `json:"RootName,omitempty"`
    13  	Description    string            `json:"Description,omitempty"`
    14  	Url            string            `json:"Url,omitempty"`
    15  	HintUrl        string            `json:"HintUrl,omitempty"`
    16  	ErrorUrl       string            `json:"ErrorUrl,omitempty"`
    17  	ExampleUrl     string            `json:"ExampleUrl,omitempty"`
    18  	SingleExample  string            `json:"SingleExample,omitempty"`
    19  	Examples       []string          `json:"Examples,omitempty"` // proves sample examples to call things correctly
    20  	ExcludeOptions *ExcludingOptions `json:"ExcludeOptions,omitempty"`
    21  	lazyMap        map[string]string
    22  }
    23  
    24  // SetSecure
    25  //
    26  //  on nil creates and returns new info with secure flag
    27  func (it *Info) SetSecure() *Info {
    28  	if it == nil {
    29  		return &Info{
    30  			ExcludeOptions: &ExcludingOptions{
    31  				IsSecureText: true,
    32  			},
    33  		}
    34  	}
    35  
    36  	it.ExcludeOptions = it.
    37  		ExcludeOptions.
    38  		SetSecure()
    39  
    40  	return it
    41  }
    42  
    43  // SetPlain
    44  //
    45  //  on nil creates and returns
    46  //  new info which is plain not secure
    47  func (it *Info) SetPlain() *Info {
    48  	if it == nil {
    49  		return &Info{} // plain text
    50  	}
    51  
    52  	it.ExcludeOptions = it.
    53  		ExcludeOptions.
    54  		SetPlainText()
    55  
    56  	return it
    57  }
    58  
    59  func (it *Info) IsIncludeRootName() bool {
    60  	return it != nil &&
    61  		it.ExcludeOptions.IsIncludeRootName() &&
    62  		it.RootName != ""
    63  }
    64  
    65  func (it *Info) IsIncludeDescription() bool {
    66  	return it != nil &&
    67  		it.ExcludeOptions.IsIncludeDescription() &&
    68  		it.Description != ""
    69  }
    70  
    71  func (it *Info) IsIncludeUrl() bool {
    72  	return it != nil &&
    73  		it.ExcludeOptions.IsIncludeUrl() &&
    74  		it.Url != ""
    75  }
    76  
    77  func (it *Info) IsIncludeHintUrl() bool {
    78  	return it != nil &&
    79  		it.ExcludeOptions.IsIncludeHintUrl() &&
    80  		it.HintUrl != ""
    81  }
    82  
    83  func (it *Info) IsIncludeErrorUrl() bool {
    84  	return it != nil &&
    85  		it.ExcludeOptions.IsIncludeErrorUrl() &&
    86  		it.ErrorUrl != ""
    87  }
    88  
    89  // IsIncludeAdditionalErrorWrap
    90  //
    91  //  returns true on null or it.ExcludeOptions.IsIncludeAdditionalErrorWrap
    92  func (it *Info) IsIncludeAdditionalErrorWrap() bool {
    93  	return it == nil ||
    94  		it.ExcludeOptions.IsIncludeAdditionalErrorWrap()
    95  }
    96  
    97  func (it *Info) IsIncludeExampleUrl() bool {
    98  	return it != nil &&
    99  		it.ExcludeOptions.IsIncludeExampleUrl() &&
   100  		it.ExampleUrl != ""
   101  }
   102  
   103  func (it *Info) IsIncludeSingleExample() bool {
   104  	return it != nil &&
   105  		it.ExcludeOptions.IsIncludeSingleExample() &&
   106  		it.SingleExample != ""
   107  }
   108  
   109  func (it *Info) IsIncludeExamples() bool {
   110  	return it != nil &&
   111  		it.ExcludeOptions.IsIncludeExamples() &&
   112  		len(it.Examples) > 0
   113  }
   114  
   115  func (it *Info) IsSecure() bool {
   116  	return it != nil && it.ExcludeOptions.IsSafeSecureText()
   117  }
   118  
   119  func (it *Info) IsPlainText() bool {
   120  	return it == nil || it.ExcludeOptions.IsIncludePayloads()
   121  }
   122  
   123  func (it *Info) IsIncludePayloads() bool {
   124  	return it == nil || it.ExcludeOptions.IsIncludePayloads()
   125  }
   126  
   127  func (it *Info) IsExcludePayload() bool {
   128  	return it != nil && it.ExcludeOptions.IsSafeSecureText()
   129  }
   130  
   131  // IsExcludeRootName
   132  //
   133  //  returns true on defined (not null) and
   134  //  it.ExcludeOptions.IsExcludeRootName
   135  //
   136  //  return false on null
   137  func (it *Info) IsExcludeRootName() bool {
   138  	return it != nil &&
   139  		it.ExcludeOptions.IsSafeExcludeRootName()
   140  }
   141  
   142  // IsExcludeDescription
   143  //
   144  //  returns true on defined (not null) and
   145  //  it.ExcludeOptions.IsExcludeDescription
   146  //
   147  //  return false on null
   148  func (it *Info) IsExcludeDescription() bool {
   149  	return it != nil &&
   150  		it.ExcludeOptions.IsSafeExcludeDescription()
   151  }
   152  
   153  // IsExcludeUrl
   154  //
   155  //  returns true on defined (not null) and
   156  //  it.ExcludeOptions.IsExcludeUrl
   157  //
   158  //  return false on null
   159  func (it *Info) IsExcludeUrl() bool {
   160  	return it != nil &&
   161  		it.ExcludeOptions.IsSafeExcludeUrl()
   162  }
   163  
   164  // IsExcludeHintUrl
   165  //
   166  //  returns true on defined (not null) and
   167  //  it.ExcludeOptions.IsExcludeHintUrl
   168  //
   169  //  return false on null
   170  func (it *Info) IsExcludeHintUrl() bool {
   171  	return it != nil &&
   172  		it.ExcludeOptions.IsSafeExcludeHintUrl()
   173  }
   174  
   175  // IsExcludeErrorUrl
   176  //
   177  //  returns true on defined (not null) and
   178  //  it.ExcludeOptions.IsExcludeErrorUrl
   179  //
   180  //  return false on null
   181  func (it *Info) IsExcludeErrorUrl() bool {
   182  	return it != nil &&
   183  		it.ExcludeOptions.IsSafeExcludeErrorUrl()
   184  }
   185  
   186  // IsExcludeAdditionalErrorWrap
   187  //
   188  //  returns true on defined (not null) and
   189  //  it.ExcludeOptions.IsExcludeAdditionalErrorWrap
   190  //
   191  //  return false on null
   192  func (it *Info) IsExcludeAdditionalErrorWrap() bool {
   193  	return it != nil &&
   194  		it.ExcludeOptions.IsSafeExcludeAdditionalErrorWrap()
   195  }
   196  
   197  // IsExcludeExampleUrl
   198  //
   199  //  return true on null
   200  func (it *Info) IsExcludeExampleUrl() bool {
   201  	return it != nil &&
   202  		it.ExcludeOptions.IsSafeExcludeExampleUrl()
   203  }
   204  
   205  // IsExcludeSingleExample
   206  //
   207  //  return true on null
   208  func (it *Info) IsExcludeSingleExample() bool {
   209  	return it != nil &&
   210  		it.ExcludeOptions.IsSafeExcludeSingleExample()
   211  }
   212  
   213  // IsExcludeExamples
   214  //
   215  //  return true on null
   216  func (it *Info) IsExcludeExamples() bool {
   217  	return it != nil &&
   218  		it.ExcludeOptions.IsSafeExcludeExamples()
   219  }
   220  
   221  func (it *Info) Name() string {
   222  	if it.IsNull() {
   223  		return ""
   224  	}
   225  
   226  	return it.RootName
   227  }
   228  
   229  func (it *Info) IsNull() bool {
   230  	return it == nil
   231  }
   232  
   233  func (it *Info) IsDefined() bool {
   234  	return it != nil && it.RootName != ""
   235  }
   236  
   237  func (it *Info) HasAnyName() bool {
   238  	return it != nil && it.RootName != ""
   239  }
   240  
   241  func (it *Info) IsName(name string) bool {
   242  	return it != nil && it.RootName == name
   243  }
   244  
   245  func (it *Info) SafeName() string {
   246  	if it.IsNull() {
   247  		return ""
   248  	}
   249  
   250  	return it.RootName
   251  }
   252  
   253  func (it *Info) SafeDescription() string {
   254  	if it.IsNull() {
   255  		return ""
   256  	}
   257  
   258  	return it.Description
   259  }
   260  
   261  func (it *Info) SafeUrl() string {
   262  	if it.IsNull() {
   263  		return ""
   264  	}
   265  
   266  	return it.Url
   267  }
   268  
   269  func (it *Info) SafeHintUrl() string {
   270  	if it.IsNull() {
   271  		return ""
   272  	}
   273  
   274  	return it.HintUrl
   275  }
   276  
   277  func (it *Info) SafeErrorUrl() string {
   278  	if it.IsNull() {
   279  		return ""
   280  	}
   281  
   282  	return it.ErrorUrl
   283  }
   284  
   285  func (it *Info) SafeExampleUrl() string {
   286  	if it.IsNull() {
   287  		return ""
   288  	}
   289  
   290  	return it.ExampleUrl
   291  }
   292  
   293  func (it *Info) SafeChainingExample() string {
   294  	if it.IsNull() {
   295  		return ""
   296  	}
   297  
   298  	return it.ExampleUrl
   299  }
   300  
   301  func (it *Info) ExamplesAsSlice() *corestr.SimpleSlice {
   302  	if it.IsNull() {
   303  		return corestr.Empty.SimpleSlice()
   304  	}
   305  
   306  	return corestr.New.SimpleSlice.Strings(it.Examples)
   307  }
   308  
   309  func (it *Info) Options() *ExcludingOptions {
   310  	if it == nil {
   311  		return &ExcludingOptions{}
   312  	}
   313  
   314  	return it.ExcludeOptions
   315  }
   316  
   317  func (it *Info) IsEmpty() bool {
   318  	return it == nil
   319  }
   320  
   321  func (it *Info) HasRootName() bool {
   322  	return it != nil && it.RootName != ""
   323  }
   324  
   325  func (it *Info) HasDescription() bool {
   326  	return it != nil && it.Description != ""
   327  }
   328  
   329  func (it *Info) HasUrl() bool {
   330  	return it != nil && it.Url != ""
   331  }
   332  
   333  func (it *Info) HasHintUrl() bool {
   334  	return it != nil && it.HintUrl != ""
   335  }
   336  
   337  func (it *Info) HasErrorUrl() bool {
   338  	return it != nil && it.ErrorUrl != ""
   339  }
   340  
   341  func (it *Info) HasExampleUrl() bool {
   342  	return it != nil && it.ExampleUrl != ""
   343  }
   344  
   345  func (it *Info) HasChainingExample() bool {
   346  	return it != nil && it.SingleExample != ""
   347  }
   348  
   349  func (it *Info) HasExamples() bool {
   350  	return it != nil && len(it.Examples) > 0
   351  }
   352  
   353  func (it *Info) HasExcludeOptions() bool {
   354  	return it != nil && !it.ExcludeOptions.IsEmpty()
   355  }
   356  
   357  func (it *Info) IsEmptyName() bool {
   358  	return it == nil || it.RootName == ""
   359  }
   360  
   361  func (it *Info) IsEmptyDescription() bool {
   362  	return it == nil || it.Description == ""
   363  }
   364  
   365  func (it *Info) IsEmptyUrl() bool {
   366  	return it == nil || it.Url == ""
   367  }
   368  
   369  func (it *Info) IsEmptyHintUrl() bool {
   370  	return it == nil || it.HintUrl == ""
   371  }
   372  
   373  func (it *Info) IsEmptyErrorUrl() bool {
   374  	return it == nil || it.ErrorUrl == ""
   375  }
   376  
   377  func (it *Info) IsEmptyExampleUrl() bool {
   378  	return it == nil || it.ExampleUrl == ""
   379  }
   380  
   381  func (it *Info) IsEmptySingleExample() bool {
   382  	return it == nil || it.SingleExample == ""
   383  }
   384  
   385  func (it *Info) IsEmptyExamples() bool {
   386  	return it == nil || len(it.Examples) == 0
   387  }
   388  
   389  func (it *Info) IsEmptyExcludeOptions() bool {
   390  	return it == nil || it.ExcludeOptions.IsZero()
   391  }
   392  
   393  func (it *Info) HasAnyItem() bool {
   394  	return it != nil
   395  }
   396  
   397  func (it Info) Json() corejson.Result {
   398  	return corejson.New(it)
   399  }
   400  
   401  func (it Info) JsonPtr() *corejson.Result {
   402  	return corejson.NewPtr(it)
   403  }
   404  
   405  func (it *Info) JsonParseSelfInject(
   406  	jsonResult *corejson.Result,
   407  ) error {
   408  	return jsonResult.Deserialize(it)
   409  }
   410  
   411  func (it Info) JsonString() string {
   412  	if it.IsNull() {
   413  		return ""
   414  	}
   415  
   416  	return it.JsonPtr().JsonString()
   417  }
   418  
   419  func (it *Info) PrettyJsonStringWithPayloads(
   420  	payloads []byte,
   421  ) string {
   422  	return corejson.
   423  		NewPtr(it.MapWithPayload(payloads)).
   424  		PrettyJsonString()
   425  }
   426  
   427  func (it *Info) PrettyJsonString() string {
   428  	if it.IsNull() {
   429  		return ""
   430  	}
   431  
   432  	return corejson.
   433  		NewPtr(it).
   434  		PrettyJsonString()
   435  }
   436  
   437  func (it *Info) LazyMapPrettyJsonString() string {
   438  	lazyMap := it.LazyMap()
   439  
   440  	return corejson.
   441  		NewPtr(lazyMap).
   442  		PrettyJsonString()
   443  }
   444  
   445  func (it Info) JsonStringMust() string {
   446  	jsonResult := it.Json()
   447  	jsonResult.MustBeSafe()
   448  
   449  	return jsonResult.JsonString()
   450  }
   451  
   452  func (it Info) ToPtr() *Info {
   453  	return &it
   454  }
   455  
   456  func (it Info) ToNonPtr() Info {
   457  	return it
   458  }
   459  
   460  func (it Info) Clone() Info {
   461  	return Info{
   462  		RootName:       it.RootName,
   463  		Description:    it.Description,
   464  		Url:            it.Url,
   465  		HintUrl:        it.HintUrl,
   466  		ErrorUrl:       it.ErrorUrl,
   467  		ExampleUrl:     it.ExampleUrl,
   468  		SingleExample:  it.SingleExample,
   469  		Examples:       it.Examples,
   470  		ExcludeOptions: it.ExcludeOptions.ClonePtr(),
   471  	}
   472  }
   473  
   474  func (it *Info) ClonePtr() *Info {
   475  	if it == nil {
   476  		return nil
   477  	}
   478  
   479  	return &Info{
   480  		RootName:       it.RootName,
   481  		Description:    it.Description,
   482  		Url:            it.Url,
   483  		HintUrl:        it.HintUrl,
   484  		ErrorUrl:       it.ErrorUrl,
   485  		ExampleUrl:     it.ExampleUrl,
   486  		SingleExample:  it.SingleExample,
   487  		Examples:       it.Examples,
   488  		ExcludeOptions: it.ExcludeOptions.ClonePtr(),
   489  	}
   490  }
   491  
   492  func (it *Info) Serialize() ([]byte, error) {
   493  	return corejson.Serialize.Raw(it)
   494  }
   495  
   496  func (it *Info) Deserialize(toPtr interface{}) (parsingErr error) {
   497  	return it.JsonPtr().Deserialize(toPtr)
   498  }
   499  
   500  func (it *Info) ExamplesAsString() (compiledString string) {
   501  	if it.IsNull() {
   502  		return ""
   503  	}
   504  
   505  	return strings.Join(
   506  		it.Examples,
   507  		constants.CommaSpace)
   508  }
   509  
   510  func (it *Info) Map() map[string]string {
   511  	if it.IsNull() || it.IsExcludeAdditionalErrorWrap() {
   512  		return map[string]string{}
   513  	}
   514  
   515  	newMap := make(
   516  		map[string]string,
   517  		constants.Capacity8)
   518  
   519  	if it.IsIncludeRootName() {
   520  		newMap[infoFieldName] = it.RootName
   521  	}
   522  
   523  	if it.IsIncludeDescription() {
   524  		newMap[infoFieldDescription] = it.Description
   525  	}
   526  
   527  	if it.IsIncludeUrl() {
   528  		newMap[infoFieldUrl] = it.Url
   529  	}
   530  
   531  	if it.IsIncludeHintUrl() {
   532  		newMap[infoFieldHintUrl] = it.HintUrl
   533  	}
   534  
   535  	if it.IsIncludeErrorUrl() {
   536  		newMap[infoFieldErrorUrl] = it.ErrorUrl
   537  	}
   538  
   539  	if it.IsIncludeExampleUrl() {
   540  		newMap[infoFieldExampleUrl] = it.ExampleUrl
   541  	}
   542  
   543  	if it.IsIncludeSingleExample() {
   544  		newMap[infoFieldSingleExample] = it.SingleExample
   545  	}
   546  
   547  	if it.IsIncludeExamples() {
   548  		newMap[infoFieldExamples] = it.ExamplesAsString()
   549  	}
   550  
   551  	return newMap
   552  }
   553  
   554  func (it *Info) MapWithPayload(
   555  	payloads []byte,
   556  ) map[string]string {
   557  	compiledMap := it.Map()
   558  
   559  	if it.IsIncludePayloads() {
   560  		compiledMap[payloadsField] = corejson.BytesToString(payloads)
   561  	}
   562  
   563  	return compiledMap
   564  }
   565  
   566  func (it *Info) LazyMapWithPayload(
   567  	payloads []byte,
   568  ) map[string]string {
   569  	compiledMap := it.LazyMap()
   570  
   571  	if it.IsIncludePayloads() {
   572  		compiledMap[payloadsField] = corejson.BytesToString(payloads)
   573  	}
   574  
   575  	return compiledMap
   576  }
   577  
   578  func (it *Info) MapWithPayloadAsAny(
   579  	payloadsAny interface{},
   580  ) map[string]string {
   581  	compiledMap := it.Map()
   582  
   583  	if it.IsExcludePayload() {
   584  		return compiledMap
   585  	}
   586  
   587  	jsonResult := corejson.
   588  		AnyTo.
   589  		SerializedJsonResult(payloadsAny)
   590  
   591  	if jsonResult.HasError() {
   592  		compiledMap[payloadsErrField] = jsonResult.MeaningfulErrorMessage()
   593  	}
   594  
   595  	compiledMap[payloadsField] = jsonResult.JsonString()
   596  
   597  	return compiledMap
   598  }
   599  
   600  func (it *Info) LazyMapWithPayloadAsAny(
   601  	payloadsAny interface{},
   602  ) map[string]string {
   603  	compiledMap := it.LazyMap()
   604  
   605  	if it.IsExcludePayload() {
   606  		return compiledMap
   607  	}
   608  
   609  	jsonResult := corejson.
   610  		AnyTo.
   611  		SerializedJsonResult(payloadsAny)
   612  
   613  	if jsonResult.HasError() {
   614  		compiledMap[payloadsErrField] = jsonResult.MeaningfulErrorMessage()
   615  	}
   616  
   617  	compiledMap[payloadsField] = jsonResult.JsonString()
   618  
   619  	return compiledMap
   620  }
   621  
   622  func (it *Info) LazyMap() map[string]string {
   623  	if it.IsNull() || it.IsExcludeAdditionalErrorWrap() {
   624  		return map[string]string{}
   625  	}
   626  
   627  	if it.lazyMap != nil {
   628  		return it.lazyMap
   629  	}
   630  
   631  	it.lazyMap = it.Map()
   632  
   633  	return it.lazyMap
   634  }
   635  
   636  func (it *Info) String() string {
   637  	if it == nil {
   638  		return ""
   639  	}
   640  
   641  	return it.PrettyJsonString()
   642  }
   643  
   644  func (it Info) AsJsonContractsBinder() corejson.JsonContractsBinder {
   645  	return &it
   646  }