github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/encoding/writer.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 "github.com/zntrio/harp/v2/pkg/sdk/ioutil" 32 ) 33 34 // ----------------------------------------------------------------------------- 35 36 // NewWriter returns the appropriate writer implementation according to given encoding. 37 func NewWriter(w io.Writer, encoding string) (io.WriteCloser, error) { 38 // Normalize input 39 encoding = strings.TrimSpace(strings.ToLower(encoding)) 40 41 var encoderWriter io.WriteCloser 42 43 // Apply transformation 44 switch encoding { 45 case "identity": 46 encoderWriter = ioutil.NopCloserWriter(w) 47 case "hex", "base16": 48 encoderWriter = ioutil.NopCloserWriter(hex.NewEncoder(w)) 49 case "base32": 50 encoderWriter = base32.NewEncoder(base32.StdEncoding, w) 51 case "base32hex": 52 encoderWriter = base32.NewEncoder(base32.HexEncoding, w) 53 case "base62": 54 encoderWriter = base62.NewEncoder(base62.StdEncoding, w) 55 case "base64": 56 encoderWriter = base64.NewEncoder(base64.StdEncoding, w) 57 case "base64raw": 58 encoderWriter = base64.NewEncoder(base64.RawStdEncoding, w) 59 case "base64url": 60 encoderWriter = base64.NewEncoder(base64.URLEncoding, w) 61 case "base64urlraw": 62 encoderWriter = base64.NewEncoder(base64.RawURLEncoding, w) 63 case "base85": 64 encoderWriter = ascii85.NewEncoder(w) 65 default: 66 return nil, fmt.Errorf("unhandled encoding strategy %q", encoding) 67 } 68 69 // No error 70 return encoderWriter, nil 71 }