github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/s3select/json/args.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package json
    19  
    20  import (
    21  	"encoding/xml"
    22  	"fmt"
    23  	"strings"
    24  )
    25  
    26  const (
    27  	document = "document"
    28  	lines    = "lines"
    29  
    30  	defaultRecordDelimiter = "\n"
    31  )
    32  
    33  // ReaderArgs - represents elements inside <InputSerialization><JSON/> in request XML.
    34  type ReaderArgs struct {
    35  	ContentType string `xml:"Type"`
    36  	unmarshaled bool
    37  }
    38  
    39  // IsEmpty - returns whether reader args is empty or not.
    40  func (args *ReaderArgs) IsEmpty() bool {
    41  	return !args.unmarshaled
    42  }
    43  
    44  // UnmarshalXML - decodes XML data.
    45  func (args *ReaderArgs) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    46  	// Make subtype to avoid recursive UnmarshalXML().
    47  	type subReaderArgs ReaderArgs
    48  	parsedArgs := subReaderArgs{}
    49  	if err := d.DecodeElement(&parsedArgs, &start); err != nil {
    50  		return err
    51  	}
    52  
    53  	parsedArgs.ContentType = strings.ToLower(parsedArgs.ContentType)
    54  	switch parsedArgs.ContentType {
    55  	case document, lines:
    56  	default:
    57  		return errInvalidJSONType(fmt.Errorf("invalid ContentType '%v'", parsedArgs.ContentType))
    58  	}
    59  
    60  	*args = ReaderArgs(parsedArgs)
    61  	args.unmarshaled = true
    62  	return nil
    63  }
    64  
    65  // WriterArgs - represents elements inside <OutputSerialization><JSON/> in request XML.
    66  type WriterArgs struct {
    67  	RecordDelimiter string `xml:"RecordDelimiter"`
    68  	unmarshaled     bool
    69  }
    70  
    71  // IsEmpty - returns whether writer args is empty or not.
    72  func (args *WriterArgs) IsEmpty() bool {
    73  	return !args.unmarshaled
    74  }
    75  
    76  // UnmarshalXML - decodes XML data.
    77  func (args *WriterArgs) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    78  	// Make subtype to avoid recursive UnmarshalXML().
    79  	type subWriterArgs WriterArgs
    80  	parsedArgs := subWriterArgs{}
    81  	if err := d.DecodeElement(&parsedArgs, &start); err != nil {
    82  		return err
    83  	}
    84  
    85  	switch len(parsedArgs.RecordDelimiter) {
    86  	case 0:
    87  		parsedArgs.RecordDelimiter = defaultRecordDelimiter
    88  	case 1, 2:
    89  	default:
    90  		return fmt.Errorf("invalid RecordDelimiter '%v'", parsedArgs.RecordDelimiter)
    91  	}
    92  
    93  	*args = WriterArgs(parsedArgs)
    94  	args.unmarshaled = true
    95  	return nil
    96  }