Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame] | 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 tu, ok := data.(encoding.TextUnmarshaler); ok { |
| 43 | err := tu.UnmarshalText(b) |
| 44 | if err != nil { |
| 45 | return fmt.Errorf("text consumer: %v", err) |
| 46 | } |
| 47 | |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | t := reflect.TypeOf(data) |
| 52 | if data != nil && t.Kind() == reflect.Ptr { |
| 53 | v := reflect.Indirect(reflect.ValueOf(data)) |
| 54 | if t.Elem().Kind() == reflect.String { |
| 55 | v.SetString(string(b)) |
| 56 | return nil |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return fmt.Errorf("%v (%T) is not supported by the TextConsumer, %s", |
| 61 | data, data, "can be resolved by supporting TextUnmarshaler interface") |
| 62 | }) |
| 63 | } |
| 64 | |
| 65 | // TextProducer creates a new text producer |
| 66 | func TextProducer() Producer { |
| 67 | return ProducerFunc(func(writer io.Writer, data interface{}) error { |
| 68 | if writer == nil { |
| 69 | return errors.New("TextProducer requires a writer") // early exit |
| 70 | } |
| 71 | |
| 72 | if data == nil { |
| 73 | return errors.New("no data given to produce text from") |
| 74 | } |
| 75 | |
| 76 | if tm, ok := data.(encoding.TextMarshaler); ok { |
| 77 | txt, err := tm.MarshalText() |
| 78 | if err != nil { |
| 79 | return fmt.Errorf("text producer: %v", err) |
| 80 | } |
| 81 | _, err = writer.Write(txt) |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | if str, ok := data.(error); ok { |
| 86 | _, err := writer.Write([]byte(str.Error())) |
| 87 | return err |
| 88 | } |
| 89 | |
| 90 | if str, ok := data.(fmt.Stringer); ok { |
| 91 | _, err := writer.Write([]byte(str.String())) |
| 92 | return err |
| 93 | } |
| 94 | |
| 95 | v := reflect.Indirect(reflect.ValueOf(data)) |
| 96 | if t := v.Type(); t.Kind() == reflect.Struct || t.Kind() == reflect.Slice { |
| 97 | b, err := swag.WriteJSON(data) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | _, err = writer.Write(b) |
| 102 | return err |
| 103 | } |
| 104 | if v.Kind() != reflect.String { |
| 105 | return fmt.Errorf("%T is not a supported type by the TextProducer", data) |
| 106 | } |
| 107 | |
| 108 | _, err := writer.Write([]byte(v.String())) |
| 109 | return err |
| 110 | }) |
| 111 | } |