This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/pkg/e621/model/pool.go
2023-11-15 13:21:13 +01:00

75 lines
2.2 KiB
Go

package model
import (
"fmt"
"strconv"
"strings"
)
type PoolCategory string
type PoolOrder string
type PoolIDs []int64
const (
Series PoolCategory = "series"
Collection PoolCategory = "collection"
)
const (
PoolName PoolOrder = "name"
CreatedAt PoolOrder = "created_at"
UpdatedAt PoolOrder = "updated_at"
PostCount PoolOrder = "post_count"
)
type Pool struct {
ID int64 `json:"id" csv:"id"`
Name string `json:"name" csv:"name"`
CreatedAt string `json:"created_at" csv:"created_at"`
UpdatedAt string `json:"updated_at" csv:"updated_at"`
CreatorID int64 `json:"creator_id" csv:"creator_id"`
Description string `json:"description" csv:"description"`
IsActive bool `json:"is_active" csv:"is_active"`
Category PoolCategory `json:"category" csv:"category"`
PostIDS PoolIDs `json:"post_ids" csv:"post_ids"`
CreatorName string `json:"creator_name" csv:"-"`
PostCount int64 `json:"post_count" csv:"-"`
}
// UnmarshalCSV parses a CSV-formatted string containing pool IDs and populates the PoolIDs receiver.
//
// This method is designed to unmarshal a CSV-formatted string, where pool IDs are separated by commas.
// It trims the surrounding curly braces and splits the string to extract individual pool IDs.
// The parsed IDs are then converted to int64 and assigned to the PoolIDs receiver.
//
// Parameters:
// - csv: The CSV-formatted string containing pool IDs.
//
// Returns:
// - error: An error encountered during the unmarshaling process, if any.
func (poolIDs *PoolIDs) UnmarshalCSV(csv string) error {
// Trim the surrounding curly braces
csv = strings.TrimPrefix(csv, "{")
csv = strings.TrimSuffix(csv, "}")
// Split the CSV string into individual pool IDs
ids := strings.Split(csv, ",")
var localPoolIDs PoolIDs
// Iterate through each ID, parse it to int64, and append to the localPoolIDs
for _, id := range ids {
if id != "" {
int64ID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return fmt.Errorf("failed to parse pool ID '%s': %v", id, err)
}
localPoolIDs = append(localPoolIDs, int64ID)
}
}
// Assign the parsed IDs to the receiver
*poolIDs = localPoolIDs
return nil
}