...

Source file src/github.com/launchdarkly/go-sdk-common/v3/ldvalue/optionals_text_conversion.go

Documentation: github.com/launchdarkly/go-sdk-common/v3/ldvalue

     1  package ldvalue
     2  
     3  import "strconv"
     4  
     5  // Note: the implementations of encoding.TextMarshaler and encoding.TextUnmarshaler are provided
     6  // for convenience for use with general-purpose parsing/configuration tools. LaunchDarkly SDK code
     7  // does not use these methods.
     8  
     9  // String returns a human-readable string representation of the value.
    10  //
    11  // Currently, this is defined as being either "true", "false, or "[none]" if it has no value.
    12  // However, the specific representation format is subject to change and should not be relied on in
    13  // code; it is intended for convenience in logging or debugging.
    14  func (o OptionalBool) String() string {
    15  	if value, ok := o.optValue.get(); ok {
    16  		if value {
    17  			return trueString
    18  		}
    19  		return falseString
    20  	}
    21  	return noneDescription
    22  }
    23  
    24  // String returns a human-readable string representation of the value.
    25  //
    26  // Currently, this is defined as being either a numeric string or "[none]" if it has no value.
    27  // However, the specific representation format is subject to change and should not be relied on in
    28  // code; it is intended for convenience in logging or debugging.
    29  func (o OptionalInt) String() string {
    30  	if value, ok := o.optValue.get(); ok {
    31  		return strconv.Itoa(value)
    32  	}
    33  	return noneDescription
    34  }
    35  
    36  // String returns a human-readable string representation of the value.
    37  //
    38  // Currently, this is defined as being either the string value itself or "[none]" if it has no value.
    39  // However, the specific representation format is subject to change and should not be relied on in
    40  // code; it is intended for convenience in logging or debugging.
    41  //
    42  // Since String() is used by methods like fmt.Printf, if you want to use the actual string value
    43  // of the OptionalString instead of this special representation, you should call StringValue():
    44  //
    45  //	s := OptionalString{}
    46  //	fmt.Printf("it is '%s'", s) // prints "it is '[none]'"
    47  //	fmt.Printf("it is '%s'", s.StringValue()) // prints "it is ''"
    48  func (o OptionalString) String() string {
    49  	if value, ok := o.optValue.get(); ok {
    50  		if value == "" {
    51  			return "[empty]"
    52  		}
    53  		return value
    54  	}
    55  	return noneDescription
    56  }
    57  
    58  // MarshalText implements the encoding.TextMarshaler interface.
    59  //
    60  // This may be useful with packages that support describing arbitrary types via that interface.
    61  //
    62  // The behavior for OptionalBool is that a true or false value produces the string "true" or
    63  // "false", and an undefined value produces an empty string.
    64  func (o OptionalBool) MarshalText() ([]byte, error) {
    65  	if value, ok := o.optValue.get(); ok {
    66  		if value {
    67  			return trueBytes, nil
    68  		}
    69  		return falseBytes, nil
    70  	}
    71  	return []byte(""), nil
    72  }
    73  
    74  // MarshalText implements the encoding.TextMarshaler interface.
    75  //
    76  // This may be useful with packages that support describing arbitrary types via that interface.
    77  //
    78  // The behavior for OptionalBool is that a true or false value produces a decimal numeric
    79  // string, and an undefined value produces an empty string.
    80  func (o OptionalInt) MarshalText() ([]byte, error) {
    81  	if value, ok := o.optValue.get(); ok {
    82  		return []byte(strconv.Itoa(value)), nil
    83  	}
    84  	return []byte(""), nil
    85  }
    86  
    87  // MarshalText implements the encoding.TextMarshaler interface.
    88  //
    89  // This may be useful with packages that support describing arbitrary types via that interface.
    90  //
    91  // The behavior for OptionalString is that a defined string value produces the same string,
    92  // and an undefined value produces nil.
    93  func (o OptionalString) MarshalText() ([]byte, error) {
    94  	if value, ok := o.optValue.get(); ok {
    95  		return []byte(value), nil
    96  	}
    97  	return nil, nil
    98  }
    99  
   100  // UnmarshalText implements the encoding.TextUnmarshaler interface.
   101  //
   102  // This may be useful with packages that support parsing arbitrary types via that interface,
   103  // such as gcfg.
   104  //
   105  // If the input data is nil or empty, the result is an empty OptionalBool{}. Otherwise, it
   106  // recognizes "true" or "false" as valid inputs and returns an error for all other values.
   107  //
   108  // This allows OptionalBool to be used with packages that can parse text content, such as gcfg.
   109  func (o *OptionalBool) UnmarshalText(text []byte) error {
   110  	if len(text) == 0 {
   111  		*o = OptionalBool{}
   112  		return nil
   113  	}
   114  	return o.UnmarshalJSON(text)
   115  }
   116  
   117  // UnmarshalText implements the encoding.TextUnmarshaler interface.
   118  //
   119  // This may be useful with packages that support parsing arbitrary types via that interface,
   120  // such as gcfg.
   121  //
   122  // If the input data is nil or empty, the result is an empty OptionalInt{}. Otherwise, it
   123  // uses strconv.Atoi() to parse a numeric value.
   124  //
   125  // This allows OptionalInt to be used with packages that can parse text content, such as gcfg.
   126  func (o *OptionalInt) UnmarshalText(text []byte) error {
   127  	if len(text) == 0 {
   128  		*o = OptionalInt{}
   129  		return nil
   130  	}
   131  	n, err := strconv.Atoi(string(text))
   132  	if err != nil {
   133  		return err
   134  	}
   135  	*o = NewOptionalInt(n)
   136  	return nil
   137  }
   138  
   139  // UnmarshalText implements the encoding.TextUnmarshaler interface.
   140  //
   141  // This may be useful with packages that support parsing arbitrary types via that interface,
   142  // such as gcfg.
   143  //
   144  // If the byte slice is nil, rather than zero-length, it will set the target value to an empty
   145  // OptionalString{}. Otherwise, it will set it to a string value.
   146  func (o *OptionalString) UnmarshalText(text []byte) error {
   147  	if text == nil {
   148  		*o = OptionalString{}
   149  	} else {
   150  		*o = NewOptionalString(string(text))
   151  	}
   152  	return nil
   153  }
   154  

View as plain text