|
| 1 | +package configio |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/benchttp/engine/benchttp" |
| 12 | +) |
| 13 | + |
| 14 | +type ( |
| 15 | + converter func(representation, *benchttp.Runner) error |
| 16 | + requestConverter func(representation, *http.Request) error |
| 17 | +) |
| 18 | + |
| 19 | +var fieldConverters = []converter{ |
| 20 | + func(repr representation, dst *benchttp.Runner) error { |
| 21 | + req := dst.Request |
| 22 | + if req == nil { |
| 23 | + req = &http.Request{} |
| 24 | + } |
| 25 | + for _, p := range requestConverters { |
| 26 | + if err := p(repr, req); err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + } |
| 30 | + dst.Request = req |
| 31 | + return nil |
| 32 | + }, |
| 33 | + func(repr representation, dst *benchttp.Runner) error { |
| 34 | + for _, p := range runnerParsers { |
| 35 | + if err := p(repr, dst); err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + } |
| 39 | + return nil |
| 40 | + }, |
| 41 | +} |
| 42 | + |
| 43 | +var requestParser = func(repr representation, dst *benchttp.Runner) error { |
| 44 | + req := &http.Request{} |
| 45 | + for _, fieldParser := range requestConverters { |
| 46 | + if err := fieldParser(repr, req); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + } |
| 50 | + dst.Request = req |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +var requestConverters = []requestConverter{ |
| 55 | + func(repr representation, dst *http.Request) error { |
| 56 | + return setString(repr.Request.Method, &dst.Method) |
| 57 | + }, |
| 58 | + func(repr representation, dst *http.Request) error { |
| 59 | + if rawURL := repr.Request.URL; rawURL != nil { |
| 60 | + parsedURL, err := parseAndBuildURL(*rawURL, repr.Request.QueryParams) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf(`configio: invalid url: %q`, *rawURL) |
| 63 | + } |
| 64 | + dst.URL = parsedURL |
| 65 | + } |
| 66 | + return nil |
| 67 | + }, |
| 68 | + func(repr representation, dst *http.Request) error { |
| 69 | + if header := repr.Request.Header; len(header) != 0 { |
| 70 | + httpHeader := http.Header{} |
| 71 | + for key, val := range header { |
| 72 | + httpHeader[key] = val |
| 73 | + } |
| 74 | + dst.Header = httpHeader |
| 75 | + } |
| 76 | + return nil |
| 77 | + }, |
| 78 | + func(repr representation, dst *http.Request) error { |
| 79 | + if body := repr.Request.Body; body != nil { |
| 80 | + switch body.Type { |
| 81 | + case "raw": |
| 82 | + dst.Body = io.NopCloser(bytes.NewReader([]byte(body.Content))) |
| 83 | + default: |
| 84 | + return errors.New(`configio: request.body.type: only "raw" accepted`) |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | +var runnerParsers = map[string]converter{ |
| 92 | + "requests": func(repr representation, dst *benchttp.Runner) error { |
| 93 | + return setInt(repr.Runner.Requests, &dst.Requests) |
| 94 | + }, |
| 95 | + "concurrency": func(repr representation, dst *benchttp.Runner) error { |
| 96 | + return setInt(repr.Runner.Concurrency, &dst.Concurrency) |
| 97 | + }, |
| 98 | + "interval": func(repr representation, dst *benchttp.Runner) error { |
| 99 | + return setOptionalDuration(repr.Runner.Interval, &dst.Interval) |
| 100 | + }, |
| 101 | + "requestTimeout": func(repr representation, dst *benchttp.Runner) error { |
| 102 | + return setOptionalDuration(repr.Runner.RequestTimeout, &dst.RequestTimeout) |
| 103 | + }, |
| 104 | + "globalTimeout": func(repr representation, dst *benchttp.Runner) error { |
| 105 | + return setOptionalDuration(repr.Runner.GlobalTimeout, &dst.GlobalTimeout) |
| 106 | + }, |
| 107 | +} |
| 108 | + |
| 109 | +func setInt(src, dst *int) error { |
| 110 | + if src != nil { |
| 111 | + *dst = *src |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func setString(src, dst *string) error { |
| 117 | + if src != nil { |
| 118 | + *dst = *src |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func setOptionalDuration(src *string, dst *time.Duration) error { |
| 124 | + if src == nil { |
| 125 | + return nil |
| 126 | + } |
| 127 | + parsed, err := parseOptionalDuration(*src) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + *dst = parsed |
| 132 | + return nil |
| 133 | +} |
0 commit comments