github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_decoder.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package funcs
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  
    12  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    13  	"github.com/GuanceCloud/platypus/pkg/ast"
    14  	"github.com/GuanceCloud/platypus/pkg/engine/runtime"
    15  	"github.com/GuanceCloud/platypus/pkg/errchain"
    16  	"golang.org/x/text/encoding"
    17  	"golang.org/x/text/encoding/simplifiedchinese"
    18  	"golang.org/x/text/encoding/unicode"
    19  )
    20  
    21  var errUnknownCharacterEncoding = errors.New("unknown character encoding")
    22  
    23  type Decoder struct {
    24  	decoder *encoding.Decoder
    25  }
    26  
    27  func NewDecoder(enc string) (*Decoder, error) {
    28  	var decoder *encoding.Decoder
    29  
    30  	switch enc {
    31  	case "'utf-8'":
    32  		decoder = unicode.UTF8.NewDecoder()
    33  	case "utf-16le", "'utf-16le'":
    34  		decoder = unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
    35  	case "utf-16be", "'utf-16be'":
    36  		decoder = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder()
    37  	case "gbk", "'gbk'":
    38  		decoder = simplifiedchinese.GBK.NewDecoder()
    39  	case "gb18030", "'gb18030'":
    40  		decoder = simplifiedchinese.GB18030.NewDecoder()
    41  	default:
    42  		return nil, errUnknownCharacterEncoding
    43  	}
    44  
    45  	return &Decoder{decoder: decoder}, nil
    46  }
    47  
    48  func Decode(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    49  	var codeType string
    50  
    51  	key, err := getKeyName(funcExpr.Param[0])
    52  	if err != nil {
    53  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    54  	}
    55  
    56  	keyVal, err := ctx.GetKeyConv2Str(key)
    57  	if err != nil {
    58  		l.Debug(err)
    59  		return nil
    60  	}
    61  	switch funcExpr.Param[1].NodeType { //nolint:exhaustive
    62  	case ast.TypeAttrExpr, ast.TypeIdentifier:
    63  		key, err := getKeyName(funcExpr.Param[1])
    64  		if err != nil {
    65  			return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[1].StartPos())
    66  		}
    67  		keyVal, err := ctx.GetKeyConv2Str(key)
    68  		if err != nil {
    69  			l.Debug(err)
    70  			return nil
    71  		}
    72  		codeType = keyVal
    73  	case ast.TypeStringLiteral:
    74  		codeType = funcExpr.Param[1].StringLiteral().Val
    75  	default:
    76  		return runtime.NewRunError(ctx, fmt.Sprintf(
    77  			"expect AttrExpr, Identifier or StringLiteral, got %s",
    78  			funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
    79  	}
    80  
    81  	encode, err := NewDecoder(codeType)
    82  	if err != nil {
    83  		l.Debug(err)
    84  		return nil
    85  	}
    86  
    87  	newcont, err := encode.decoder.String(keyVal)
    88  	if err != nil {
    89  		l.Debug(err)
    90  		return nil
    91  	}
    92  
    93  	_ = addKey2PtWithVal(ctx.InData(), key, newcont,
    94  		ast.String, ptinput.KindPtDefault)
    95  	return nil
    96  }
    97  
    98  func DecodeChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    99  	if len(funcExpr.Param) < 2 || len(funcExpr.Param) > 2 {
   100  		return runtime.NewRunError(ctx, fmt.Sprintf(
   101  			"func %s expected 2", funcExpr.Name), funcExpr.NamePos)
   102  	}
   103  
   104  	if _, err := getKeyName(funcExpr.Param[0]); err != nil {
   105  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
   106  	}
   107  
   108  	switch funcExpr.Param[1].NodeType { //nolint:exhaustive
   109  	case ast.TypeAttrExpr, ast.TypeIdentifier, ast.TypeStringLiteral:
   110  	default:
   111  		return runtime.NewRunError(ctx, fmt.Sprintf(
   112  			"expect AttrExpr, Identifier or StringLiteral, got %s",
   113  			funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos())
   114  	}
   115  	return nil
   116  }