gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/anyTo.go (about) 1 package corejson 2 3 import ( 4 "errors" 5 6 "gitlab.com/evatix-go/core/internal/reflectinternal" 7 ) 8 9 type anyTo struct{} 10 11 // SerializedJsonResult 12 // 13 // Casting happens: 14 // - self or self pointer returns directly 15 // - []Bytes to Result 16 // - string (json) to Result 17 // - Jsoner to Result 18 // - bytesSerializer to Result 19 // - error to Result 20 // - AnyItem 21 func (it anyTo) SerializedJsonResult( 22 fromAny interface{}, 23 ) *Result { 24 if reflectinternal.IsNull(fromAny) { 25 return &Result{ 26 Error: errors.New("nil object given"), 27 TypeName: reflectinternal.SafeTypeName(fromAny), 28 } 29 } 30 31 switch castedTo := fromAny.(type) { 32 case Result: 33 return castedTo.Ptr() 34 case *Result: 35 return castedTo 36 case []byte: 37 return NewResult.UsingBytesTypePtr( 38 castedTo, 39 "RawBytes", 40 ) 41 case string: 42 return NewResult.UsingBytesTypePtr( 43 []byte(castedTo), 44 "RawString", 45 ) 46 case Jsoner: 47 return castedTo.JsonPtr() 48 case bytesSerializer: 49 return NewResult.UsingSerializer(castedTo) 50 case error: 51 if castedTo == nil || castedTo.Error() == "" { 52 // empty err 53 return NewResult.UsingBytesTypePtr( 54 []byte{}, 55 errTypeString, 56 ) 57 } 58 59 return NewResult.UsingTypePlusString( 60 errTypeString, // type 61 castedTo.Error()) // json string 62 } 63 64 return Serialize.Apply( 65 fromAny) 66 } 67 68 func (it anyTo) SerializedRaw( 69 fromAny interface{}, 70 ) (allBytes []byte, err error) { 71 return it.SerializedJsonResult(fromAny).Raw() 72 } 73 74 // SerializedString 75 // 76 // accepted types (usages SerializedJsonResult): 77 // - Result, *Result 78 // - []byte 79 // - string 80 // - jsoner 81 // - bytesSerializer 82 // - anyItem 83 func (it anyTo) SerializedString( 84 fromAny interface{}, 85 ) (serializedString string, err error) { 86 jsonResult := it.SerializedJsonResult(fromAny) 87 88 if jsonResult.HasError() { 89 return "", jsonResult.MeaningfulError() 90 } 91 92 return jsonResult.JsonString(), nil 93 } 94 95 // SerializedSafeString 96 // 97 // accepted types (usages SerializedJsonResult): 98 // - Result, *Result 99 // - []byte 100 // - string 101 // - jsoner 102 // - bytesSerializer 103 // - anyItem 104 // 105 // Warning: 106 // swallows error, important data convert must not go into this. 107 func (it anyTo) SerializedSafeString( 108 fromAny interface{}, 109 ) (serializedString string) { 110 jsonResult := it.SerializedJsonResult(fromAny) 111 112 if jsonResult.HasError() { 113 return "" 114 } 115 116 return jsonResult.JsonString() 117 } 118 119 func (it anyTo) SerializedStringMust( 120 fromAny interface{}, 121 ) (serializedString string) { 122 jsonResult := it.SerializedJsonResult(fromAny) 123 jsonResult.MustBeSafe() 124 125 return jsonResult.JsonString() 126 } 127 128 // SafeJsonString 129 // 130 // warning : swallows error 131 func (it anyTo) SafeJsonString( 132 anyItem interface{}, 133 ) string { 134 jsonResult := New(anyItem) 135 136 return jsonResult.JsonString() 137 } 138 139 func (it anyTo) PrettyStringWithError( 140 anyItem interface{}, 141 ) (string, error) { 142 switch casted := anyItem.(type) { 143 case string: 144 return casted, nil 145 case []byte: 146 return BytesToPrettyString(casted), nil 147 case Result: 148 if casted.HasError() { 149 return casted.PrettyJsonString(), casted.MeaningfulError() 150 } 151 152 return casted.PrettyJsonString(), nil 153 case *Result: 154 if casted.HasError() { 155 return casted.PrettyJsonString(), casted.MeaningfulError() 156 } 157 158 return casted.PrettyJsonString(), nil 159 } 160 161 jsonResult := New(anyItem) 162 163 return jsonResult.PrettyJsonString(), jsonResult.MeaningfulError() 164 } 165 166 // SafeJsonPrettyString 167 // 168 // warning : swallows error 169 func (it anyTo) SafeJsonPrettyString( 170 anyItem interface{}, 171 ) string { 172 switch casted := anyItem.(type) { 173 case string: 174 return casted 175 case []byte: 176 return BytesToPrettyString(casted) 177 case Result: 178 return casted.PrettyJsonString() 179 case *Result: 180 return casted.PrettyJsonString() 181 } 182 183 jsonResult := New(anyItem) 184 185 return jsonResult.PrettyJsonString() 186 } 187 188 func (it anyTo) JsonString( 189 anyItem interface{}, 190 ) string { 191 switch casted := anyItem.(type) { 192 case string: 193 return casted 194 case []byte: 195 return BytesToString(casted) 196 case Result: 197 return casted.JsonString() 198 case *Result: 199 return casted.JsonString() 200 } 201 202 jsonResult := New(anyItem) 203 204 return jsonResult.JsonString() 205 } 206 207 func (it anyTo) JsonStringWithErr( 208 anyItem interface{}, 209 ) (jsonString string, parsingErr error) { 210 switch casted := anyItem.(type) { 211 case string: 212 return casted, nil 213 case []byte: 214 return BytesToString(casted), nil 215 case Result: 216 if casted.HasError() { 217 return casted.JsonString(), casted.MeaningfulError() 218 } 219 220 return casted.JsonString(), nil 221 case *Result: 222 if casted.HasError() { 223 return casted.JsonString(), casted.MeaningfulError() 224 } 225 226 return casted.JsonString(), nil 227 } 228 229 jsonResult := New(anyItem) 230 231 return jsonResult.JsonString(), jsonResult.MeaningfulError() 232 } 233 234 func (it anyTo) JsonStringMust( 235 anyItem interface{}, 236 ) string { 237 jsonStr, err := it.JsonStringWithErr(anyItem) 238 239 if err != nil { 240 panic(err) 241 } 242 243 return jsonStr 244 } 245 246 func (it anyTo) PrettyStringMust( 247 anyItem interface{}, 248 ) string { 249 jsonPretty, err := it.JsonStringWithErr( 250 anyItem) 251 252 if err != nil { 253 panic(err) 254 } 255 256 return jsonPretty 257 } 258 259 func (it anyTo) UsingSerializer( 260 serializer bytesSerializer, 261 ) *Result { 262 return NewResult.UsingSerializer( 263 serializer) 264 } 265 266 // SerializedFieldsMap 267 // 268 // usages json to bytes then use json to create fields map 269 func (it anyTo) SerializedFieldsMap( 270 anyItem interface{}, 271 ) (fieldsMap map[string]interface{}, parsingErr error) { 272 return it.SerializedJsonResult(anyItem). 273 DeserializedFieldsToMap() 274 }