github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encoding/reader.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package encoding 19 20 import ( 21 "encoding/ascii85" 22 "encoding/base32" 23 "encoding/base64" 24 "encoding/hex" 25 "fmt" 26 "io" 27 "strings" 28 29 "github.com/lytics/base62" 30 ) 31 32 // ----------------------------------------------------------------------------- 33 34 // NewReader returns a reader implementation matching the given encoding strategy. 35 func NewReader(r io.Reader, encoding string) (io.Reader, error) { 36 // Normalize input 37 encoding = strings.TrimSpace(strings.ToLower(encoding)) 38 39 var decoderReader io.Reader 40 41 // Apply transformation 42 switch encoding { 43 case "identity": 44 decoderReader = r 45 case "hex", "base16": 46 decoderReader = hex.NewDecoder(r) 47 case "base32": 48 decoderReader = base32.NewDecoder(base32.StdEncoding, r) 49 case "base32hex": 50 decoderReader = base32.NewDecoder(base32.HexEncoding, r) 51 case "base62": 52 decoderReader = base62.NewDecoder(base62.StdEncoding, r) 53 case "base64": 54 decoderReader = base64.NewDecoder(base64.StdEncoding, r) 55 case "base64raw": 56 decoderReader = base64.NewDecoder(base64.RawStdEncoding, r) 57 case "base64url": 58 decoderReader = base64.NewDecoder(base64.URLEncoding, r) 59 case "base64urlraw": 60 decoderReader = base64.NewDecoder(base64.RawURLEncoding, r) 61 case "base85": 62 decoderReader = ascii85.NewDecoder(r) 63 default: 64 return nil, fmt.Errorf("unhandled decoding strategy %q", encoding) 65 } 66 67 // No error 68 return decoderReader, nil 69 }