github.com/schmorrison/Zoho@v1.1.4/shifts/shifts.go (about)

     1  package shifts
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"strings"
     7  	"time"
     8  
     9  	zoho "github.com/schmorrison/Zoho"
    10  )
    11  
    12  type Module = string
    13  
    14  const (
    15  	EmployeesModule    Module = "employees" // CRUD
    16  	AvailabilityModule Module = "schedules" // CRUD - Shifts, Availability
    17  	availabilityModule Module = "availability"
    18  	shiftsModule       Module = "shifts"
    19  
    20  	TimesheetsModule Module = "timesheets" // CRUD
    21  	SettingsModule   Module = "settings"   // CRUD - Settings (Schedules, Positions, Job Sites)
    22  	schedulesModule  Module = "schedules"
    23  	positionsModule  Module = "positions"
    24  	jobSitesModule   Module = "jobsites"
    25  
    26  	TimeoffModule Module = "timeoff" // CRUD
    27  )
    28  
    29  // API is used for interacting with the Zoho Shifts API
    30  // the exposed methods are primarily access to Shifts modules which provide access to Shifts Methods
    31  type API struct {
    32  	*zoho.Zoho
    33  	id string
    34  }
    35  
    36  // New returns a *shifts.API with the provided zoho.Zoho as an embedded field
    37  func New(z *zoho.Zoho) *API {
    38  	id := func() string {
    39  		var id []byte
    40  		keyspace := "abcdefghijklmnopqrutuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    41  		for i := 0; i < 25; i++ {
    42  			source := rand.NewSource(time.Now().UnixNano())
    43  			rnd := rand.New(source)
    44  			id = append(id, keyspace[rnd.Intn(len(keyspace))])
    45  		}
    46  		return string(id)
    47  	}()
    48  
    49  	return &API{
    50  		Zoho: z,
    51  		id:   id,
    52  	}
    53  }
    54  
    55  const (
    56  	timeLayout = "2006-01-02T15:04:05Z"
    57  	dateLayout = "2006-01-02"
    58  )
    59  
    60  type Time time.Time
    61  
    62  func (t Time) MarshalJSON() (b []byte, err error) {
    63  	if t.IsZero() {
    64  		return []byte{'"', '"'}, nil
    65  	}
    66  
    67  	return []byte(t.String()), nil
    68  }
    69  
    70  func (t *Time) UnmarshalJSON(b []byte) (err error) {
    71  	s := strings.Trim(string(b), `"`)
    72  	if s == "null" || s == "\"null\"" || s == "" {
    73  		return nil
    74  	}
    75  
    76  	tm, err := time.Parse(timeLayout, s)
    77  	if err != nil {
    78  		return fmt.Errorf("failed to parse shifts.Time from JSON: %s", err)
    79  	}
    80  	*t = Time(tm)
    81  	return nil
    82  }
    83  
    84  func (t Time) String() string {
    85  	tm := time.Time(t)
    86  	return fmt.Sprintf("%q", tm.In(time.UTC).Format(timeLayout))
    87  }
    88  
    89  func (t Time) IsZero() bool {
    90  	return time.Time(t).IsZero()
    91  }
    92  
    93  type Date time.Time
    94  
    95  func (d Date) MarshalJSON() (b []byte, err error) {
    96  	if d.IsZero() {
    97  		return []byte{'"', '"'}, nil
    98  	}
    99  
   100  	return []byte(d.String()), nil
   101  }
   102  
   103  func (d *Date) UnmarshalJSON(b []byte) (err error) {
   104  	s := strings.Trim(string(b), `"`)
   105  	dm, err := time.Parse(dateLayout, s)
   106  	if err != nil {
   107  		return fmt.Errorf("failed to parse shifts.Time from JSON: %s", err)
   108  	}
   109  	*d = Date(dm)
   110  	return nil
   111  }
   112  
   113  func (d Date) String() string {
   114  	tm := time.Time(d)
   115  	return fmt.Sprintf("%q", tm.In(time.UTC).Format(dateLayout))
   116  }
   117  
   118  func (d Date) IsZero() bool {
   119  	return time.Time(d).IsZero()
   120  }