Add IntOrFalse type to handle polymorphic API responses
Kanboard API returns int (ID) on success and false (bool) on failure for create operations. Add IntOrFalse type that handles both cases and update CreateTask, CreateComment, CreateTag, CreateTaskFile, and CreateTaskLink to use it.
This commit is contained in:
parent
59252a3f63
commit
a34a40cb12
7 changed files with 63 additions and 10 deletions
28
types.go
28
types.go
|
|
@ -2,6 +2,7 @@ package kanboard
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
|
@ -105,6 +106,33 @@ func (i *StringInt64) UnmarshalJSON(data []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// IntOrFalse is an int that can be unmarshaled from a JSON int or false.
|
||||
// Kanboard API returns false on failure, int (ID) on success for create operations.
|
||||
type IntOrFalse int
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
func (i *IntOrFalse) UnmarshalJSON(data []byte) error {
|
||||
// Try as int first (success case)
|
||||
var n int
|
||||
if err := json.Unmarshal(data, &n); err == nil {
|
||||
*i = IntOrFalse(n)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Try as bool (failure case: false)
|
||||
var b bool
|
||||
if err := json.Unmarshal(data, &b); err == nil {
|
||||
if b {
|
||||
*i = 1 // true shouldn't happen, but handle it
|
||||
} else {
|
||||
*i = 0
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot unmarshal %s into IntOrFalse", data)
|
||||
}
|
||||
|
||||
// Project represents a Kanboard project (board).
|
||||
type Project struct {
|
||||
ID StringInt `json:"id"`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue