...

Source file src/github.com/go-openapi/runtime/text.go

Documentation: github.com/go-openapi/runtime

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package runtime
    16  
    17  import (
    18  	"bytes"
    19  	"encoding"
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  	"reflect"
    24  
    25  	"github.com/go-openapi/swag"
    26  )
    27  
    28  // TextConsumer creates a new text consumer
    29  func TextConsumer() Consumer {
    30  	return ConsumerFunc(func(reader io.Reader, data interface{}) error {
    31  		if reader == nil {
    32  			return errors.New("TextConsumer requires a reader") // early exit
    33  		}
    34  
    35  		buf := new(bytes.Buffer)
    36  		_, err := buf.ReadFrom(reader)
    37  		if err != nil {
    38  			return err
    39  		}
    40  		b := buf.Bytes()
    41  
    42  		// If the buffer is empty, no need to unmarshal it, which causes a panic.
    43  		if len(b) == 0 {
    44  			return nil
    45  		}
    46  
    47  		if tu, ok := data.(encoding.TextUnmarshaler); ok {
    48  			err := tu.UnmarshalText(b)
    49  			if err != nil {
    50  				return fmt.Errorf("text consumer: %v", err)
    51  			}
    52  
    53  			return nil
    54  		}
    55  
    56  		t := reflect.TypeOf(data)
    57  		if data != nil && t.Kind() == reflect.Ptr {
    58  			v := reflect.Indirect(reflect.ValueOf(data))
    59  			if t.Elem().Kind() == reflect.String {
    60  				v.SetString(string(b))
    61  				return nil
    62  			}
    63  		}
    64  
    65  		return fmt.Errorf("%v (%T) is not supported by the TextConsumer, %s",
    66  			data, data, "can be resolved by supporting TextUnmarshaler interface")
    67  	})
    68  }
    69  
    70  // TextProducer creates a new text producer
    71  func TextProducer() Producer {
    72  	return ProducerFunc(func(writer io.Writer, data interface{}) error {
    73  		if writer == nil {
    74  			return errors.New("TextProducer requires a writer") // early exit
    75  		}
    76  
    77  		if data == nil {
    78  			return errors.New("no data given to produce text from")
    79  		}
    80  
    81  		if tm, ok := data.(encoding.TextMarshaler); ok {
    82  			txt, err := tm.MarshalText()
    83  			if err != nil {
    84  				return fmt.Errorf("text producer: %v", err)
    85  			}
    86  			_, err = writer.Write(txt)
    87  			return err
    88  		}
    89  
    90  		if str, ok := data.(error); ok {
    91  			_, err := writer.Write([]byte(str.Error()))
    92  			return err
    93  		}
    94  
    95  		if str, ok := data.(fmt.Stringer); ok {
    96  			_, err := writer.Write([]byte(str.String()))
    97  			return err
    98  		}
    99  
   100  		v := reflect.Indirect(reflect.ValueOf(data))
   101  		if t := v.Type(); t.Kind() == reflect.Struct || t.Kind() == reflect.Slice {
   102  			b, err := swag.WriteJSON(data)
   103  			if err != nil {
   104  				return err
   105  			}
   106  			_, err = writer.Write(b)
   107  			return err
   108  		}
   109  		if v.Kind() != reflect.String {
   110  			return fmt.Errorf("%T is not a supported type by the TextProducer", data)
   111  		}
   112  
   113  		_, err := writer.Write([]byte(v.String()))
   114  		return err
   115  	})
   116  }
   117  

View as plain text