From a82641774504eb7a629957ae37e74bb820071bfd Mon Sep 17 00:00:00 2001 From: Teajey <21069848+Teajey@users.noreply.github.com> Date: Mon, 25 Aug 2025 21:18:04 +0900 Subject: [PATCH] feat: api snapshot pre-commit --- core/api.snap.txt | 105 ++++++++++++++++++++++++++++++ core/scripts/api_changes_since | 5 ++ core/scripts/api_snapshot | 3 + core/scripts/strip_godoc_comments | 55 ++++++++++++++++ scripts/pre-commit | 13 ++++ 5 files changed, 181 insertions(+) create mode 100644 core/api.snap.txt create mode 100755 core/scripts/api_changes_since create mode 100755 core/scripts/api_snapshot create mode 100755 core/scripts/strip_godoc_comments create mode 100755 scripts/pre-commit diff --git a/core/api.snap.txt b/core/api.snap.txt new file mode 100644 index 0000000..cdf2b38 --- /dev/null +++ b/core/api.snap.txt @@ -0,0 +1,105 @@ +package lishwist // import "lishwist/core" + + +VARIABLES + +var ErrorUsernameTaken = errors.New("Username is taken") + +FUNCTIONS + +func Init(dataSourceName string) error +func PrintTables(d *sql.DB) +func PrintViews(d *sql.DB) + +TYPES + +type Admin struct { + // Has unexported fields. +} + +func (a *Admin) AddUserToGroup(userId, groupId string) error + +func (a *Admin) CreateGroup(name string, reference string) (*Group, error) + +func (*Admin) GetUser(id string) (*User, error) + +func (a *Admin) ListGroups() ([]Group, error) + +func (*Admin) ListUsers() ([]User, error) + +func (a *Admin) RemoveUserFromGroup(userId, groupId string) error + +func (u *Admin) UserSetLive(userReference string, setting bool) error + +type ErrorInvalidCredentials error + +type Group struct { + Id string + Name string + Reference string + Members []User +} + +func GetGroupByReference(reference string) (*Group, error) + +func (g *Group) MemberIndex(userId string) int + +type Session struct { + User +} + +func Login(username, password string) (*Session, error) + +func SessionFromUsername(username string) (*Session, error) + +func (s *Session) Admin() *Admin + +func (s *Session) ClaimWishes(claims, unclaims []string) error + +func (s *Session) CompleteWishes(claims []string) error + +func (s *Session) GetGroupByReference(reference string) (*Group, error) + +func (u *Session) GetGroups() ([]Group, error) + +func (s *Session) GetOthersWishes(userReference string) ([]Wish, error) + +func (s *Session) GetWishes() ([]Wish, error) + +func (s *Session) MakeWish(name string) error + +func (s *Session) RecindWishesForUser(ids ...string) error + +func (s *Session) RevokeWishes(ids ...string) error + +func (u *Session) SuggestWishForUser(otherUserReference string, wishName string) error + +type User struct { + Id string + NormalName string + Name string + Reference string + IsAdmin bool + IsLive bool +} + +func GetUserByReference(reference string) (*User, error) + +func Register(username, newPassword string) (*User, error) + +func (u *User) GetTodo() ([]Wish, error) + +func (u *User) WishCount() (int, error) + +type Wish struct { + Id string + Name string + ClaimantId string `json:",omitempty"` + ClaimantName string `json:",omitempty"` + Sent bool + RecipientId string `json:",omitempty"` + RecipientName string `json:",omitempty"` + RecipientRef string `json:",omitempty"` + CreatorId string `json:",omitempty"` + CreatorName string `json:",omitempty"` +} diff --git a/core/scripts/api_changes_since b/core/scripts/api_changes_since new file mode 100755 index 0000000..976cd3b --- /dev/null +++ b/core/scripts/api_changes_since @@ -0,0 +1,5 @@ +#!/bin/sh + +set -x + +git diff --exit-code $1 api.snap.txt $(find . -name '*_test.go' -type f) diff --git a/core/scripts/api_snapshot b/core/scripts/api_snapshot new file mode 100755 index 0000000..900d281 --- /dev/null +++ b/core/scripts/api_snapshot @@ -0,0 +1,3 @@ +#!/bin/bash + +go doc -all | ./scripts/strip_godoc_comments diff --git a/core/scripts/strip_godoc_comments b/core/scripts/strip_godoc_comments new file mode 100755 index 0000000..814aef5 --- /dev/null +++ b/core/scripts/strip_godoc_comments @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +import fileinput +from collections import deque +from typing import Iterator +from typing import TypeVar + +T = TypeVar("T") + + +def window(it: Iterator[T], size: int = 2) -> Iterator[list[T]]: + if size < 2: + raise ValueError("Window size must be at least 2") + + window: deque[T] = deque(maxlen=size) + + for _ in range(size): + try: + window.append(next(it)) + except StopIteration: + return + + yield list(window) + + for item in it: + window.append(item) + yield list(window) + + +struct = False +func = False +for line, next_line in window(fileinput.input()): + line = line.removesuffix("\n") + + if line.startswith("type ") and line.endswith(" struct {"): + struct = True + elif struct and line.endswith("}"): + struct = False + elif line.startswith("func"): + func = True + elif next_line[:4].strip() != "": + func = False + + if struct: + if ( + line != "" + and not line.lstrip().startswith("//") + or "Has unexported fields" in line + ): + print(line) + elif func: + if line != "" and not line.startswith(" "): + print(line) + else: + print(line) diff --git a/scripts/pre-commit b/scripts/pre-commit new file mode 100755 index 0000000..b181fb2 --- /dev/null +++ b/scripts/pre-commit @@ -0,0 +1,13 @@ +#!/bin/sh + +cd core/ + +APISNAP=api.snap.txt + +./scripts/api_snapshot > $APISNAP +git diff --quiet $APISNAP + +if [[ $? -ne 0 ]]; then + echo "There are unstaged changes to $APISNAP" + exit 1 +fi