mirror of
https://github.com/aykhans/dodo.git
synced 2025-07-02 00:16:20 +00:00
✨ Add yaml file reader to config
This commit is contained in:
@ -66,6 +66,28 @@ func (body *Body) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (body *Body) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var data any
|
||||
if err := unmarshal(&data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch v := data.(type) {
|
||||
case string:
|
||||
*body = []string{v}
|
||||
case []any:
|
||||
var slice []string
|
||||
for _, item := range v {
|
||||
slice = append(slice, fmt.Sprintf("%v", item))
|
||||
}
|
||||
*body = slice
|
||||
default:
|
||||
return fmt.Errorf("invalid type for Body: %T (should be string or []string)", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (body *Body) Set(value string) error {
|
||||
*body = append(*body, value)
|
||||
return nil
|
||||
|
@ -11,13 +11,22 @@ const (
|
||||
|
||||
type ConfigFile string
|
||||
|
||||
func (config ConfigFile) String() string {
|
||||
return string(config)
|
||||
func (configFile ConfigFile) String() string {
|
||||
return string(configFile)
|
||||
}
|
||||
|
||||
func (config ConfigFile) LocationType() FileLocationType {
|
||||
if strings.HasPrefix(string(config), "http://") || strings.HasPrefix(string(config), "https://") {
|
||||
func (configFile ConfigFile) LocationType() FileLocationType {
|
||||
if strings.HasPrefix(string(configFile), "http://") || strings.HasPrefix(string(configFile), "https://") {
|
||||
return FileLocationTypeRemoteHTTP
|
||||
}
|
||||
return FileLocationTypeLocal
|
||||
}
|
||||
|
||||
func (configFile ConfigFile) Extension() string {
|
||||
i := strings.LastIndex(configFile.String(), ".")
|
||||
if i == -1 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return configFile.String()[i+1:]
|
||||
}
|
||||
|
@ -56,6 +56,23 @@ func (cookies Cookies) String() string {
|
||||
return string(buffer.Bytes())
|
||||
}
|
||||
|
||||
func (cookies *Cookies) AppendByKey(key, value string) {
|
||||
if item := cookies.GetValue(key); item != nil {
|
||||
*item = append(*item, value)
|
||||
} else {
|
||||
*cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: []string{value}})
|
||||
}
|
||||
}
|
||||
|
||||
func (cookies Cookies) GetValue(key string) *[]string {
|
||||
for i := range cookies {
|
||||
if cookies[i].Key == key {
|
||||
return &cookies[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cookies *Cookies) UnmarshalJSON(b []byte) error {
|
||||
var data []map[string]any
|
||||
if err := json.Unmarshal(b, &data); err != nil {
|
||||
@ -82,6 +99,31 @@ func (cookies *Cookies) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cookies *Cookies) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var raw []map[string]any
|
||||
if err := unmarshal(&raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, param := range raw {
|
||||
for key, value := range param {
|
||||
switch parsed := value.(type) {
|
||||
case string:
|
||||
*cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: []string{parsed}})
|
||||
case []any:
|
||||
var values []string
|
||||
for _, v := range parsed {
|
||||
if str, ok := v.(string); ok {
|
||||
values = append(values, str)
|
||||
}
|
||||
}
|
||||
*cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: values})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cookies *Cookies) Set(value string) error {
|
||||
parts := strings.SplitN(value, "=", 2)
|
||||
switch len(parts) {
|
||||
@ -95,20 +137,3 @@ func (cookies *Cookies) Set(value string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cookies *Cookies) AppendByKey(key, value string) {
|
||||
if item := cookies.GetValue(key); item != nil {
|
||||
*item = append(*item, value)
|
||||
} else {
|
||||
*cookies = append(*cookies, KeyValue[string, []string]{Key: key, Value: []string{value}})
|
||||
}
|
||||
}
|
||||
|
||||
func (cookies Cookies) GetValue(key string) *[]string {
|
||||
for i := range cookies {
|
||||
if cookies[i].Key == key {
|
||||
return &cookies[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -34,3 +34,24 @@ func (timeout *Timeout) UnmarshalJSON(b []byte) error {
|
||||
func (timeout Timeout) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(timeout.Duration.String())
|
||||
}
|
||||
|
||||
func (timeout *Timeout) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var v any
|
||||
if err := unmarshal(&v); err != nil {
|
||||
return err
|
||||
}
|
||||
switch value := v.(type) {
|
||||
case float64:
|
||||
timeout.Duration = time.Duration(value)
|
||||
return nil
|
||||
case string:
|
||||
var err error
|
||||
timeout.Duration, err = time.ParseDuration(value)
|
||||
if err != nil {
|
||||
return errors.New("Timeout is invalid (e.g. 400ms, 1s, 5m, 1h)")
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return errors.New("Timeout is invalid (e.g. 400ms, 1s, 5m, 1h)")
|
||||
}
|
||||
}
|
||||
|
@ -56,6 +56,23 @@ func (headers Headers) String() string {
|
||||
return string(buffer.Bytes())
|
||||
}
|
||||
|
||||
func (headers *Headers) AppendByKey(key, value string) {
|
||||
if item := headers.GetValue(key); item != nil {
|
||||
*item = append(*item, value)
|
||||
} else {
|
||||
*headers = append(*headers, KeyValue[string, []string]{Key: key, Value: []string{value}})
|
||||
}
|
||||
}
|
||||
|
||||
func (headers Headers) GetValue(key string) *[]string {
|
||||
for i := range headers {
|
||||
if headers[i].Key == key {
|
||||
return &headers[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (headers *Headers) UnmarshalJSON(b []byte) error {
|
||||
var data []map[string]any
|
||||
if err := json.Unmarshal(b, &data); err != nil {
|
||||
@ -82,6 +99,31 @@ func (headers *Headers) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (headers *Headers) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var raw []map[string]any
|
||||
if err := unmarshal(&raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, param := range raw {
|
||||
for key, value := range param {
|
||||
switch parsed := value.(type) {
|
||||
case string:
|
||||
*headers = append(*headers, KeyValue[string, []string]{Key: key, Value: []string{parsed}})
|
||||
case []any:
|
||||
var values []string
|
||||
for _, v := range parsed {
|
||||
if str, ok := v.(string); ok {
|
||||
values = append(values, str)
|
||||
}
|
||||
}
|
||||
*headers = append(*headers, KeyValue[string, []string]{Key: key, Value: values})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (headers *Headers) Set(value string) error {
|
||||
parts := strings.SplitN(value, ":", 2)
|
||||
switch len(parts) {
|
||||
@ -95,20 +137,3 @@ func (headers *Headers) Set(value string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (headers *Headers) AppendByKey(key, value string) {
|
||||
if item := headers.GetValue(key); item != nil {
|
||||
*item = append(*item, value)
|
||||
} else {
|
||||
*headers = append(*headers, KeyValue[string, []string]{Key: key, Value: []string{value}})
|
||||
}
|
||||
}
|
||||
|
||||
func (headers Headers) GetValue(key string) *[]string {
|
||||
for i := range headers {
|
||||
if headers[i].Key == key {
|
||||
return &headers[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -56,6 +56,23 @@ func (params Params) String() string {
|
||||
return string(buffer.Bytes())
|
||||
}
|
||||
|
||||
func (params *Params) AppendByKey(key, value string) {
|
||||
if item := params.GetValue(key); item != nil {
|
||||
*item = append(*item, value)
|
||||
} else {
|
||||
*params = append(*params, KeyValue[string, []string]{Key: key, Value: []string{value}})
|
||||
}
|
||||
}
|
||||
|
||||
func (params Params) GetValue(key string) *[]string {
|
||||
for i := range params {
|
||||
if params[i].Key == key {
|
||||
return ¶ms[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (params *Params) UnmarshalJSON(b []byte) error {
|
||||
var data []map[string]any
|
||||
if err := json.Unmarshal(b, &data); err != nil {
|
||||
@ -82,6 +99,31 @@ func (params *Params) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (params *Params) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var raw []map[string]any
|
||||
if err := unmarshal(&raw); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, param := range raw {
|
||||
for key, value := range param {
|
||||
switch parsed := value.(type) {
|
||||
case string:
|
||||
*params = append(*params, KeyValue[string, []string]{Key: key, Value: []string{parsed}})
|
||||
case []any:
|
||||
var values []string
|
||||
for _, v := range parsed {
|
||||
if str, ok := v.(string); ok {
|
||||
values = append(values, str)
|
||||
}
|
||||
}
|
||||
*params = append(*params, KeyValue[string, []string]{Key: key, Value: values})
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (params *Params) Set(value string) error {
|
||||
parts := strings.SplitN(value, "=", 2)
|
||||
switch len(parts) {
|
||||
@ -95,20 +137,3 @@ func (params *Params) Set(value string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (params *Params) AppendByKey(key, value string) {
|
||||
if item := params.GetValue(key); item != nil {
|
||||
*item = append(*item, value)
|
||||
} else {
|
||||
*params = append(*params, KeyValue[string, []string]{Key: key, Value: []string{value}})
|
||||
}
|
||||
}
|
||||
|
||||
func (params Params) GetValue(key string) *[]string {
|
||||
for i := range params {
|
||||
if params[i].Key == key {
|
||||
return ¶ms[i].Value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -75,6 +75,36 @@ func (proxies *Proxies) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (proxies *Proxies) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var data any
|
||||
if err := unmarshal(&data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch v := data.(type) {
|
||||
case string:
|
||||
parsed, err := url.Parse(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*proxies = []url.URL{*parsed}
|
||||
case []any:
|
||||
var urls []url.URL
|
||||
for _, item := range v {
|
||||
url, err := url.Parse(item.(string))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
urls = append(urls, *url)
|
||||
}
|
||||
*proxies = urls
|
||||
default:
|
||||
return fmt.Errorf("invalid type for Body: %T (should be URL or []URL)", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (proxies *Proxies) Set(value string) error {
|
||||
parsedURL, err := url.Parse(value)
|
||||
if err != nil {
|
||||
|
@ -25,6 +25,21 @@ func (requestURL *RequestURL) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (requestURL *RequestURL) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var urlStr string
|
||||
if err := unmarshal(&urlStr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsedURL, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return errors.New("Request URL is invalid")
|
||||
}
|
||||
|
||||
requestURL.URL = *parsedURL
|
||||
return nil
|
||||
}
|
||||
|
||||
func (requestURL RequestURL) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(requestURL.URL.String())
|
||||
}
|
||||
|
Reference in New Issue
Block a user