github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/internal/ekaletter/field_helpers.go (about)

     1  // Copyright © 2019. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekaletter
     7  
     8  import (
     9  	"strconv"
    10  )
    11  
    12  // KeyOrUnnamed returns LetterField's 'Key' field if it's not empty or string
    13  // "unnamed_<unnamedIdx>". Has generated code for 'unnamedIdx' <= 32.
    14  // Increments 'unnamedIdx' before use it (if it have to be used).
    15  //
    16  // Returns an empty string if 'unnamedIdx' == nil and it must be used.
    17  func (f LetterField) KeyOrUnnamed(unnamedIdx *int16) string {
    18  
    19  	if f.Key != "" {
    20  		return f.Key
    21  	}
    22  
    23  	if unnamedIdx == nil {
    24  		return ""
    25  	}
    26  
    27  	*unnamedIdx++
    28  
    29  	if *unnamedIdx < 0 || *unnamedIdx > 32 {
    30  		return "unnamed_" + strconv.Itoa(int(*unnamedIdx))
    31  	}
    32  
    33  	switch *unnamedIdx {
    34  	case 0:
    35  		return "unnamed_00"
    36  	case 1:
    37  		return "unnamed_01"
    38  	case 2:
    39  		return "unnamed_02"
    40  	case 3:
    41  		return "unnamed_03"
    42  	case 4:
    43  		return "unnamed_04"
    44  	case 5:
    45  		return "unnamed_05"
    46  	case 6:
    47  		return "unnamed_06"
    48  	case 7:
    49  		return "unnamed_07"
    50  	case 8:
    51  		return "unnamed_08"
    52  	case 9:
    53  		return "unnamed_09"
    54  	case 10:
    55  		return "unnamed_10"
    56  	case 11:
    57  		return "unnamed_11"
    58  	case 12:
    59  		return "unnamed_12"
    60  	case 13:
    61  		return "unnamed_13"
    62  	case 14:
    63  		return "unnamed_14"
    64  	case 15:
    65  		return "unnamed_15"
    66  	case 16:
    67  		return "unnamed_16"
    68  	case 17:
    69  		return "unnamed_17"
    70  	case 18:
    71  		return "unnamed_18"
    72  	case 19:
    73  		return "unnamed_19"
    74  	case 20:
    75  		return "unnamed_20"
    76  	case 21:
    77  		return "unnamed_21"
    78  	case 22:
    79  		return "unnamed_22"
    80  	case 23:
    81  		return "unnamed_23"
    82  	case 24:
    83  		return "unnamed_24"
    84  	case 25:
    85  		return "unnamed_25"
    86  	case 26:
    87  		return "unnamed_26"
    88  	case 27:
    89  		return "unnamed_27"
    90  	case 28:
    91  		return "unnamed_28"
    92  	case 29:
    93  		return "unnamed_29"
    94  	case 30:
    95  		return "unnamed_30"
    96  	case 31:
    97  		return "unnamed_31"
    98  	case 32:
    99  		return "unnamed_32"
   100  	default:
   101  		return ""
   102  	}
   103  }
   104  
   105  // RemoveVary removes "sign of variety" for the current LetterField.
   106  // Returns true if it was and removed now. False otherwise.
   107  func (f *LetterField) RemoveVary() bool {
   108  	if f.Key != "" && f.Key[len(f.Key)-1] == '?' {
   109  		f.Key = f.Key[:len(f.Key)-1]
   110  		return true
   111  	}
   112  	return false
   113  }