From 04821a6e7752d6852bc1d283a88f05f7abecae87 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sun, 28 Sep 2025 09:48:40 -0700 Subject: Add a gob API --- internal/api/api.go | 64 ++++++++++++++++++++++++++++++++++++++++ internal/api/errorcode_string.go | 28 ++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 internal/api/api.go create mode 100644 internal/api/errorcode_string.go (limited to 'internal/api') diff --git a/internal/api/api.go b/internal/api/api.go new file mode 100644 index 0000000..7c8472a --- /dev/null +++ b/internal/api/api.go @@ -0,0 +1,64 @@ +// Package api contains type definitions for the gob API. +package api + +import "encoding/gob" + +type ErrorCode int + +//go:generate go tool stringer -type=ErrorCode +const ( + Ok ErrorCode = iota + Unauthenticated // missing credentials + PermissionDenied // invalid credentials, or login required + BadRequest // incorrect request format (try gob) + NotFound // requested resource was not found + Internal // server encountered an unexpected error +) + +func (c ErrorCode) Error() string { + return c.String() +} + +type Response struct { + Status ErrorCode + Err string + Ok any +} + +type LoginResponse struct { + Token string +} + +type ListNotesRequest struct{} + +type ListNotesResponse struct { + Notes []string +} + +type CreateNoteRequestStream struct { + Chunk []byte +} + +type CreateNoteResponse struct { + Name string +} + +type ReadNoteRequest struct { + Note string +} + +type ReadNoteResponseStream struct { + Chunk []byte +} + +type DeleteNoteRequest struct{} + +type DeleteNoteResponse struct{} + +func init() { + gob.Register(&LoginResponse{}) + gob.Register(&ListNotesResponse{}) + gob.Register(&CreateNoteResponse{}) + gob.Register(&ReadNoteResponseStream{}) + gob.Register(&DeleteNoteResponse{}) +} diff --git a/internal/api/errorcode_string.go b/internal/api/errorcode_string.go new file mode 100644 index 0000000..2b4a294 --- /dev/null +++ b/internal/api/errorcode_string.go @@ -0,0 +1,28 @@ +// Code generated by "stringer -type=ErrorCode"; DO NOT EDIT. + +package api + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[Ok-0] + _ = x[Unauthenticated-1] + _ = x[PermissionDenied-2] + _ = x[BadRequest-3] + _ = x[NotFound-4] + _ = x[Internal-5] +} + +const _ErrorCode_name = "OkUnauthenticatedPermissionDeniedBadRequestNotFoundInternal" + +var _ErrorCode_index = [...]uint8{0, 2, 17, 33, 43, 51, 59} + +func (i ErrorCode) String() string { + if i < 0 || i >= ErrorCode(len(_ErrorCode_index)-1) { + return "ErrorCode(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _ErrorCode_name[_ErrorCode_index[i]:_ErrorCode_index[i+1]] +} -- cgit v1.3.1