github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/azid/text.go (about) 1 package azid 2 3 import ( 4 "github.com/rez-go/crock32" 5 6 errors "github.com/alloyzeus/go-azfl/azerrs" 7 ) 8 9 // TextMarshalable is an interface definition for objects which able to 10 // provide an azid-text representation of itself. 11 type TextMarshalable interface { 12 AZIDText() string 13 } 14 15 // A TextUnmarshalable is an object which able to load an 16 // azid-text representation into itself. 17 type TextUnmarshalable interface { 18 UnmarshalAZIDText(s string) error 19 } 20 21 var ( 22 // TextEncode encodes azid-bin ref-key. 23 TextEncode = crock32.EncodeLower 24 ) 25 26 // TextDecode decodes azid-bin ref-key from a string. 27 func TextDecode(s string) ([]byte, error) { 28 var dataEncoded []byte 29 var checksumEncoded []byte 30 inputAsBytes := []byte(s) 31 for i, c := range inputAsBytes { 32 if c == 'U' || c == 'u' { 33 if i+1 < len(inputAsBytes) { 34 if c = inputAsBytes[i+1]; c == 'N' || c == 'n' { 35 dataEncoded = inputAsBytes[:i] 36 checksumEncoded = inputAsBytes[i+2 : i+4] 37 break 38 } 39 } else { 40 dataEncoded = inputAsBytes[:i] 41 break 42 } 43 } 44 } 45 if dataEncoded == nil { 46 dataEncoded = []byte(s) 47 } 48 dataBytes, err := crock32.Decode(string(dataEncoded)) 49 if err != nil { 50 return nil, errors.Arg1().Desc(errors.ErrValueMalformed).Wrap(err) 51 } 52 if len(checksumEncoded) == 2 { 53 //TODO: checksum 54 } 55 return dataBytes, nil 56 }