diff --git a/folders.go b/folders.go index e259ded..f181529 100644 --- a/folders.go +++ b/folders.go @@ -62,7 +62,7 @@ func QueryFoldersByIds(config Config, params params.QueryFolders, pathId []strin func CreateFoldersByFolder(config Config, params params.CreateFolders, pathId string) (resp.Folders, error) { path := fmt.Sprintf("/folders/%s/folders", pathId) - body, err := query.Values(params) + body, err := Values(params) if err != nil { log.Println(err) } diff --git a/parameters/folders.go b/parameters/folders.go index 471eeba..eff3834 100644 --- a/parameters/folders.go +++ b/parameters/folders.go @@ -13,14 +13,14 @@ type QueryFolders struct { } type CreateFolders struct { - Title string `url:"title"` - Description *string `url:"description,omitempty"` - Shareds *[]string `url:"shareds,omitempty"` - Metadata *[]Metadata `url:"metadata,omitempty"` - CustomFields *[]CustomField `url:"customFields,omitempty"` - CustomColumns *[]string `url:"customColumns,omitempty"` - Project *Project `url:"project,omitempty"` - Fields *[]string `url:"fields,omitempty"` + Title string `url:"title"` + Description string `url:"description,omitempty"` + Shareds []string `url:"shareds,omitempty"` + Metadata []Metadata `url:"metadata,omitempty"` + CustomFields []CustomField `url:"customFields,omitempty"` + CustomColumns []string `url:"customColumns,omitempty"` + Project Project `url:"project,omitempty,struct"` + Fields []string `url:"fields,omitempty"` } type CreateFoldersCopy struct { diff --git a/parameters/sharedParamaters.go b/parameters/sharedParamaters.go index ba69860..6c6eee5 100644 --- a/parameters/sharedParamaters.go +++ b/parameters/sharedParamaters.go @@ -11,11 +11,11 @@ type DateOrRange struct { } type TaskDates struct { - Type string `url:"type"` - Duration *int `url:"duration,omitempty"` - Start *string `url:"start,omitempty"` - Due *string `url:"due,omitempty"` - WorkOnWeekends *bool `url:"workOnWeekends,omitempty"` + Type string `url:"type"` + Duration int `url:"duration,omitempty"` + Start string `url:"start,omitempty"` + Due string `url:"due,omitempty"` + WorkOnWeekends bool `url:"workOnWeekends,omitempty"` } type Metadata struct { @@ -33,22 +33,22 @@ type CustomField struct { } type EffortAllocation struct { - AllocatedEffort *int `url:"allocatedEffort,omitempty"` - DailyAllocationPercentage *int `url:"dailyAllocationPercentage,omitempty"` + AllocatedEffort int `url:"allocatedEffort,omitempty"` + DailyAllocationPercentage int `url:"dailyAllocationPercentage,omitempty"` Mode string `url:"mode"` - TotalEffort *int `url:"totalEffort,omitempty"` + TotalEffort int `url:"totalEffort,omitempty"` } type Project struct { - OwnerIds *[]string `url:"ownerIds,omitempty"` - Status *string `url:"status,omitempty"` - CustomStatusId *string `url:"customStatusId,omitempty"` - StartDate *string `url:"startDate,omitempty"` - EndDate *string `url:"endDate,omitempty"` - ContractType *string `url:"contractType,omitempty"` - Budget *float64 `url:"budget,omitempty"` - OwnersAdd *[]string `url:"ownersAdd,omitempty"` - OwnersRemove *[]string `url:"ownersRemove,omitempty"` + OwnerIds []string `url:"ownerIds,omitempty"` + Status string `url:"status,omitempty"` + CustomStatusId string `url:"customStatusId,omitempty"` + StartDate string `url:"startDate,omitempty"` + EndDate string `url:"endDate,omitempty"` + ContractType string `url:"contractType,omitempty"` + Budget float64 `url:"budget,omitempty"` + OwnersAdd []string `url:"ownersAdd,omitempty"` + OwnersRemove []string `url:"ownersRemove,omitempty"` } type Avatar struct { diff --git a/parameters/tasks.go b/parameters/tasks.go index fcf427c..2abfa52 100644 --- a/parameters/tasks.go +++ b/parameters/tasks.go @@ -35,26 +35,26 @@ type QueryTasksFieldsHistory struct { } type CreateTasks struct { - Title string `url:"title"` - Description string `url:"description,omitempty"` - Status string `url:"status,omitempty"` - Importance string `url:"importance,omitempty"` - Dates *TaskDates `url:"dates,omitempty"` - Shareds []string `url:"shareds,omitempty,slice"` - Parents []string `url:"parents,omitempty"` - Responsibles []string `url:"responsibles,omitempty"` - ResponsiblePlaceholders []string `url:"resopnsiblePlaceholders,omitempty"` - Followers []string `url:"followers,omitempty"` - Follow bool `url:"follow,omitempty"` - PriorityBefore string `url:"priorityBefore,omitempty"` - PriorityAfter string `url:"priorityAfter,omitempty"` - SuperTasks []string `url:"superTasks,omitempty"` - Metadata *[]Metadata `url:"metadata,omitempty"` + Title string `url:"title"` + Description string `url:"description,omitempty"` + Status string `url:"status,omitempty"` + Importance string `url:"importance,omitempty"` + Dates TaskDates `url:"dates,omitempty,struct"` + Shareds []string `url:"shareds,omitempty,slice"` + Parents []string `url:"parents,omitempty"` + Responsibles []string `url:"responsibles,omitempty"` + ResponsiblePlaceholders []string `url:"resopnsiblePlaceholders,omitempty"` + Followers []string `url:"followers,omitempty"` + Follow bool `url:"follow,omitempty"` + PriorityBefore string `url:"priorityBefore,omitempty"` + PriorityAfter string `url:"priorityAfter,omitempty"` + SuperTasks []string `url:"superTasks,omitempty"` + Metadata []Metadata `url:"metadata,omitempty"` CustomFields []CustomField `url:"customFields,omitempty,slice+struct"` - CustomStatus string `url:"customStatus,omitempty"` - EffortAllocation *EffortAllocation `url:"effortAllocation,omitempty"` - BillingType string `url:"billingType,omitempty"` - Fields []string `url:"fields,omitempty"` + CustomStatus string `url:"customStatus,omitempty"` + EffortAllocation EffortAllocation `url:"effortAllocation,omitempty"` + BillingType string `url:"billingType,omitempty"` + Fields []string `url:"fields,omitempty"` } type ModifyTasks struct { diff --git a/query.go b/query.go index 82afdc9..af838cf 100644 --- a/query.go +++ b/query.go @@ -169,12 +169,11 @@ func reflectValue(values url.Values, val reflect.Value, scope string) error { } if sv.Kind() == reflect.Struct { - if err := reflectValue(values, sv, name); err != nil { - return err + if opts.Contains("struct") { //! Here + values.Add(name, valueString(sv, opts, sf)) } continue } - // fmt.Println(valueString(sv, opts, sf)) values.Add(name, valueString(sv, opts, sf)) } @@ -221,31 +220,23 @@ func valueString(v reflect.Value, opts tagOptions, sf reflect.StructField) strin return t.Format(time.RFC3339) } + //! Here if opts.Contains("slice") { val := v.Interface().([]string) newVal := sliceConvert(val) var inter interface{} inter = newVal return fmt.Sprint(inter) + } else if opts.Contains("struct") { + newVal := objectConvert(v) + var inter interface{} + inter = newVal + return fmt.Sprint(inter) } else if opts.Contains("slice+struct") { newVal := "[" for i := 0; i < v.Len(); i++ { - newVal += "{" val := v.Index(i) - for j := 0; j < val.NumField(); j++ { - key := val.Type().Field(j).Name - value := val.FieldByName(key).String() - if val.FieldByName(key).Kind() == reflect.Slice { - value = sliceConvert(val.FieldByName(key).Interface().([]string)) - } - // Check if value is empty - _, opts2 := parseTag(val.Type().Field(j).Tag.Get("url")) - if opts2.Contains("omitempty") && isEmptyValue(val.FieldByName(key)) { - continue - } - newVal += `"` + strcase.ToLowerCamel(key) + `":"` + value + `",` - } - newVal = newVal[:len(newVal)-1] + "}," + newVal += objectConvert(val) + "," } newVal = newVal[:len(newVal)-1] + "]" var inter interface{} @@ -256,6 +247,7 @@ func valueString(v reflect.Value, opts tagOptions, sf reflect.StructField) strin return fmt.Sprint(v.Interface()) } +//! Here func sliceConvert(sl []string) string { if len(sl) > 0 { return `["` + strings.Join(sl, `","`) + `"]` @@ -264,6 +256,28 @@ func sliceConvert(sl []string) string { } } +//! Here +func objectConvert(v reflect.Value) string { + newVal := "{" + val := v + for j := 0; j < val.NumField(); j++ { + key := val.Type().Field(j).Name + var value string // value := fmt.Sprint(val.FieldByName(key).Interface()) + if val.FieldByName(key).Kind() == reflect.Slice { + value = sliceConvert(val.FieldByName(key).Interface().([]string)) + } else { + value = `"` + fmt.Sprint(val.FieldByName(key).Interface()) + `"` + } + _, opts2 := parseTag(val.Type().Field(j).Tag.Get("url")) + if opts2.Contains("omitempty") && isEmptyValue(val.FieldByName(key)) { + continue + } + newVal += `"` + strcase.ToLowerCamel(key) + `":` + value + `,` + } + newVal = newVal[:len(newVal)-1] + "}" + return newVal +} + // isEmptyValue checks if a value should be considered empty for the purposes // of omitting fields with the "omitempty" option. func isEmptyValue(v reflect.Value) bool { diff --git a/tasks.go b/tasks.go index c39eec3..2bdf693 100644 --- a/tasks.go +++ b/tasks.go @@ -10,15 +10,6 @@ import ( query "github.com/google/go-querystring/query" ) - -func Parse(jsonString string) (string) { - test := "" - - fmt.Println("Test:", test) - - return "" -} - func QueryTasks(config Config, params params.QueryTasks) (resp.Tasks, error) { path := "/tasks" body, err := query.Values(params) @@ -29,8 +20,6 @@ func QueryTasks(config Config, params params.QueryTasks) (resp.Tasks, error) { return resp.TasksFromJSON(response) } -// Working out how to encode slices properly and objects properly - func QueryTasksByFolder(config Config, params params.QueryTasks, pathId string) (resp.Tasks, error) { path := fmt.Sprintf("/folders/%s/tasks", pathId) body, err := query.Values(params) @@ -74,27 +63,11 @@ func QueryTasksFieldsHistoryByTasks(config Config, params params.QueryTasksField func CreateTasksByFolder(config Config, params params.CreateTasks, pathId string) (resp.Tasks, error) { path := fmt.Sprintf("/folders/%s/tasks", pathId) body, err := Values(params) - - - - - if err != nil { log.Println(err) } response, _ := Post(config, path, body) return resp.TasksFromJSON(response) - - - - - // path := fmt.Sprintf("/folders/%s/tasks", pathId) - // body, err := query.Values(params) - // if err != nil { - // log.Println(err) - // } - // response, _ := Post(config, path, body) - // return resp.TasksFromJSON(response) } //----------------------- func ModifyTasksById(config Config, params params.ModifyTasks, pathId string) (resp.Tasks, error) {