storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/json/args.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package json
    18  
    19  import (
    20  	"encoding/xml"
    21  	"fmt"
    22  	"strings"
    23  )
    24  
    25  const (
    26  	document = "document"
    27  	lines    = "lines"
    28  
    29  	defaultRecordDelimiter = "\n"
    30  )
    31  
    32  // ReaderArgs - represents elements inside <InputSerialization><JSON/> in request XML.
    33  type ReaderArgs struct {
    34  	ContentType string `xml:"Type"`
    35  	unmarshaled bool
    36  }
    37  
    38  // IsEmpty - returns whether reader args is empty or not.
    39  func (args *ReaderArgs) IsEmpty() bool {
    40  	return !args.unmarshaled
    41  }
    42  
    43  // UnmarshalXML - decodes XML data.
    44  func (args *ReaderArgs) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    45  	// Make subtype to avoid recursive UnmarshalXML().
    46  	type subReaderArgs ReaderArgs
    47  	parsedArgs := subReaderArgs{}
    48  	if err := d.DecodeElement(&parsedArgs, &start); err != nil {
    49  		return err
    50  	}
    51  
    52  	parsedArgs.ContentType = strings.ToLower(parsedArgs.ContentType)
    53  	switch parsedArgs.ContentType {
    54  	case document, lines:
    55  	default:
    56  		return errInvalidJSONType(fmt.Errorf("invalid ContentType '%v'", parsedArgs.ContentType))
    57  	}
    58  
    59  	*args = ReaderArgs(parsedArgs)
    60  	args.unmarshaled = true
    61  	return nil
    62  }
    63  
    64  // WriterArgs - represents elements inside <OutputSerialization><JSON/> in request XML.
    65  type WriterArgs struct {
    66  	RecordDelimiter string `xml:"RecordDelimiter"`
    67  	unmarshaled     bool
    68  }
    69  
    70  // IsEmpty - returns whether writer args is empty or not.
    71  func (args *WriterArgs) IsEmpty() bool {
    72  	return !args.unmarshaled
    73  }
    74  
    75  // UnmarshalXML - decodes XML data.
    76  func (args *WriterArgs) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    77  	// Make subtype to avoid recursive UnmarshalXML().
    78  	type subWriterArgs WriterArgs
    79  	parsedArgs := subWriterArgs{}
    80  	if err := d.DecodeElement(&parsedArgs, &start); err != nil {
    81  		return err
    82  	}
    83  
    84  	switch len(parsedArgs.RecordDelimiter) {
    85  	case 0:
    86  		parsedArgs.RecordDelimiter = defaultRecordDelimiter
    87  	case 1, 2:
    88  	default:
    89  		return fmt.Errorf("invalid RecordDelimiter '%v'", parsedArgs.RecordDelimiter)
    90  	}
    91  
    92  	*args = WriterArgs(parsedArgs)
    93  	args.unmarshaled = true
    94  	return nil
    95  }