-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: generate typescript types from Golang models #4
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Ensure all generated files are up-to-date | ||
name: GenerateFiles | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
make-gen: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v2 | ||
with: | ||
go-version: 1.23.0 | ||
- name: Run make gen | ||
run: make gen | ||
- name: Check for unstaged files | ||
run: ./scripts/check_unstaged.sh |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
.env | ||
postgres_data/ | ||
.DS_STORE | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.PHONY: gen | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Time to learn Make! |
||
gen: dcl-attend-frontend/src/services/models.ts | ||
|
||
# Depends on any files in model | ||
dcl-attend-frontend/src/services/models.ts: $(wildcard internal/model/*) scripts/typesgen/main.go | ||
# This tmp dir would not work on windows ¯\_(ツ)_/¯ | ||
go run scripts/typesgen/main.go > /tmp/models.ts && \ | ||
mv /tmp/models.ts $@ | ||
# TODO: Run TS formatter on the file here |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Run `make gen` from the root of the repository to regenerate this file. | ||
// Code generated by 'guts'. DO NOT EDIT. | ||
|
||
// From model/attendance.go | ||
export interface Attendance { | ||
readonly id: string; | ||
readonly address: string; | ||
readonly created_at: string; | ||
readonly metaverse: MetaverseType; | ||
readonly location: string; | ||
readonly entrance_status: EntranceStatusType; | ||
} | ||
|
||
// From model/attendance.go | ||
export type EntranceStatusType = "ENTER" | "EXIT"; | ||
|
||
// From model/attendance.go | ||
export type MetaverseType = "dcl" | "hyperfy" | "irl"; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
module github.com/JollyGrin/postgres-attendance | ||
|
||
go 1.22 | ||
go 1.23.0 | ||
|
||
toolchain go1.22.9 | ||
toolchain go1.23.4 | ||
Comment on lines
-3
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this chill? Can you update your Go? |
||
|
||
require ( | ||
github.com/coder/guts v0.0.0-20241210145042-959d730444c8 | ||
github.com/jackc/pgx/v5 v5.7.1 | ||
github.com/joho/godotenv v1.5.1 | ||
github.com/lib/pq v1.10.9 | ||
github.com/sirupsen/logrus v1.9.3 | ||
) | ||
|
||
require ( | ||
github.com/dlclark/regexp2 v1.11.4 // indirect | ||
github.com/dop251/goja v0.0.0-20241024094426-79f3a7efcdbd // indirect | ||
github.com/fatih/structtag v1.2.0 // indirect | ||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect | ||
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect | ||
github.com/jackc/pgpassfile v1.0.0 // indirect | ||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect | ||
github.com/jackc/puddle/v2 v2.2.2 // indirect | ||
github.com/stretchr/testify v1.9.0 // indirect | ||
golang.org/x/crypto v0.27.0 // indirect | ||
golang.org/x/sync v0.8.0 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
golang.org/x/mod v0.22.0 // indirect | ||
golang.org/x/sync v0.9.0 // indirect | ||
golang.org/x/sys v0.27.0 // indirect | ||
golang.org/x/text v0.18.0 // indirect | ||
golang.org/x/tools v0.27.0 // indirect | ||
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
set -euo pipefail | ||
# shellcheck source=scripts/lib.sh | ||
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh" | ||
cdroot | ||
|
||
FILES="$(git ls-files --other --modified --exclude-standard)" | ||
if [[ "$FILES" != "" ]]; then | ||
mapfile -t files <<<"$FILES" | ||
|
||
log | ||
log "The following files contain unstaged changes:" | ||
log | ||
for file in "${files[@]}"; do | ||
log " - $file" | ||
done | ||
|
||
log | ||
log "These are the changes:" | ||
log | ||
for file in "${files[@]}"; do | ||
git --no-pager diff "$file" 1>&2 | ||
done | ||
|
||
log | ||
error "Unstaged changes, see above for details." | ||
fi |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/coder/guts" | ||
"github.com/coder/guts/config" | ||
) | ||
|
||
func main() { | ||
gen, err := guts.NewGolangParser() | ||
if err != nil { | ||
log.Fatalf("go parser: %v", err) | ||
} | ||
|
||
generatePackages := map[string]string{ | ||
"github.com/JollyGrin/postgres-attendance/internal/model": "", | ||
} | ||
|
||
for pkg, prefix := range generatePackages { | ||
err = gen.IncludeGenerateWithPrefix(pkg, prefix) | ||
if err != nil { | ||
log.Fatalf("include generate package %q: %v", pkg, err) | ||
} | ||
} | ||
|
||
// Standard type mappings | ||
gen.IncludeCustomDeclaration(config.StandardMappings()) | ||
|
||
ts, err := gen.ToTypescript() | ||
if err != nil { | ||
log.Fatalf("to typescript: %v", err) | ||
} | ||
|
||
ts.ApplyMutations( | ||
config.SimplifyOmitEmpty, | ||
config.ExportTypes, | ||
config.ReadOnly, | ||
) | ||
|
||
output, err := ts.Serialize() | ||
if err != nil { | ||
log.Fatalf("serialize: %v", err) | ||
} | ||
|
||
fmt.Println("// Run `make gen` from the root of the repository to regenerate this file.") | ||
fmt.Println(output) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this action works... I did not test it.