-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparameter.go
60 lines (51 loc) · 1.69 KB
/
parameter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package openmeteo
import (
"fmt"
"reflect"
"strings"
)
type Parameter struct {
Latitude *float32 `json:"latitude"`
Longitude *float32 `json:"longitude"`
Hourly *[]string `json:"hourly"`
Elevation *float32 `json:"elevation"`
Daily *[]string `json:"daily"`
CurrentWeather *bool `json:"current_weather"`
TemperatureUnit *string `json:"temperature_unit"`
WindSpeedUnit *string `json:"windspeed_unit"`
PrecipitationUnit *string `json:"precipitation_unit"`
TimeFormat *string `json:"timeformat"`
Timezone *string `json:"timezone"`
PastDays *int `json:"past_days"`
ForecastDays *int `json:"forecast_days"`
StartDate *string `json:"start_date"`
EndDate *string `json:"end_date"`
Models *[]string `json:"models"`
CellSelection *string `json:"cell_selection"`
}
func (p Parameter) ToQuery() string {
v := reflect.ValueOf(p)
var queryParams []string
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
value := v.Field(i)
if value.IsNil() {
continue
}
fieldName := field.Tag.Get("json")
fieldValue := fmt.Sprintf("%v", value.Elem().Interface())
// Check if the field value is a slice
if value.Elem().Kind() == reflect.Slice {
slice := value.Elem()
var sliceValues []string
for j := 0; j < slice.Len(); j++ {
sliceValue := fmt.Sprintf("%v", slice.Index(j).Interface())
sliceValues = append(sliceValues, sliceValue)
}
fieldValue = strings.Join(sliceValues, ",")
}
queryParam := fieldName + "=" + fieldValue
queryParams = append(queryParams, queryParam)
}
return strings.Join(queryParams, "&")
}