Skip to content

Commit

Permalink
Added ExtraActions and Dynamic GORM query driver
Browse files Browse the repository at this point in the history
  • Loading branch information
glothriel committed Mar 3, 2024
1 parent eca095c commit 24a0936
Show file tree
Hide file tree
Showing 44 changed files with 2,164 additions and 420 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/k6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
cache-to: type=gha,mode=max

- name: Run container from image
run: docker run -d -p 8080:8080 products:latest --db=/tmp/db.sqlite3
run: docker run -d -p 8080:8080 products:latest

- name: Install k6
run: |
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ jobs:
run: python .github/scripts/create-empty-tests.py

- name: Run Unit tests
run: go test -v -coverpkg=./... -race -coverprofile=coverage.out -covermode=atomic ./...
run: |
go test -v -coverpkg=./... -race -coverprofile=coverage.out.tmp -covermode=atomic ./...
cat coverage.out.tmp | grep -v "examples/" > coverage.out
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
60 changes: 28 additions & 32 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
FROM golang:alpine as builder
ARG GO_VERSION=1.21.1


RUN apk update && apk add --no-cache git ca-certificates gcc musl-dev && update-ca-certificates

ENV USER=user
ENV UID=10001

RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
"${USER}"

WORKDIR /app
COPY go.mod .
COPY go.sum .
# Build stage
FROM golang:${GO_VERSION}-bullseye AS build

RUN useradd -u 1000 nonroot -m
USER nonroot:nonroot
WORKDIR /src
COPY ./go.mod ./go.sum ./

RUN go mod download
RUN go mod verify
COPY pkg ./pkg
RUN CGO_ENABLED=1 go build \
-installsuffix 'static' \
-o /home/nonroot/app ./pkg/examples/products/main.go

# Dev stage, can be used for development using docker-compose "build.target" config
FROM build as dev
WORKDIR /home/nonroot
ARG VERSION=dev
ENV VERSION=${VERSION}
ENTRYPOINT ["/home/nonroot/app"]
CMD ["start"]

COPY pkg pkg
env CGO_ENABLED=1
RUN GOOS=linux GOARCH=amd64 go build -o /go/bin/products pkg/examples/products/main.go
RUN chmod +x /go/bin/products
FROM alpine
ENV GIN_MODE=release
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder /go/bin/products /go/bin/products
USER user:user
ENTRYPOINT ["/go/bin/products"]
# Prod stage, uses distrolless image
FROM gcr.io/distroless/base AS prod
ARG VERSION=dev
ENV VERSION=${VERSION}
USER nonroot:nonroot
COPY --from=build --chown=nonroot:nonroot /home/nonroot/app /home/nonroot/app
LABEL maintainer="https://github.com/glothriel"
ENTRYPOINT ["/home/nonroot/app"]
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,16 @@ Other gunicorn flag variants had even lower performance.
* ~~Improve unit test coverage~~
* ~~Documentation portal~~
* Add support for complex to implement types:
* time.Time
* time.Time and sql.NullTime
* time.Duration
* `list<int>`
* pointer fields, for example `*string`
* sql.Null.* fields, for example `sql.NullString`
* ~~sql.Null.* fields, for example `sql.NullString`~~
* JSON fields, both as text (sqlite) and as dedicated columns (eg. Postgres)
* Add support for partial update (PATCH)
* SliceModelFields do not work with JSONSchemaValidator
* Add support for model relations
* Add support for viewsets
* ~~Add support for viewsets~~
* Add support for authentication
* Add support for complex pagination implementations
* Add support for permissions (authorization)
Expand Down
1 change: 1 addition & 0 deletions artifacts/benchmark.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"root_group":{"name":"","path":"","id":"d41d8cd98f00b204e9800998ecf8427e","groups":[{"name":"setup","path":"::setup","id":"5c0f8025f7e0b6654089e5b00e950f1a","groups":[],"checks":[{"id":"77976cc76c32d08c6bf343022a4547eb","passes":3,"fails":0,"name":"product is created","path":"::setup::product is created"}]},{"name":"teardown","path":"::teardown","id":"a8a9942382e8ffe2e56ef29e664b8229","groups":[],"checks":[{"name":"product is deleted","path":"::teardown::product is deleted","id":"978916e86e81789dc672a1af8a257a7a","passes":3,"fails":0}]}],"checks":[]},"options":{"summaryTrendStats":["avg","min","med","max","p(90)","p(95)"],"summaryTimeUnit":"","noColor":false},"state":{"isStdOutTTY":true,"isStdErrTTY":true,"testRunDurationMs":3028.827282},"metrics":{"iterations":{"type":"counter","contains":"default","values":{"count":9105,"rate":3006.1139683038555}},"http_req_waiting":{"type":"trend","contains":"time","values":{"p(95)":0.28385869999999996,"avg":0.24414285990126153,"min":0.192764,"med":0.232434,"max":5.171587,"p(90)":0.26498}},"http_req_duration{expected_response:true}":{"type":"trend","contains":"time","values":{"max":5.222222,"p(90)":0.30291900000000005,"p(95)":0.32406630000000003,"avg":0.2788634223806909,"min":0.219933,"med":0.266726}},"http_req_failed":{"type":"rate","contains":"default","values":{"fails":9115,"rate":0,"passes":0}},"iteration_duration":{"type":"trend","contains":"time","values":{"avg":0.31991549017239557,"min":0.258136,"med":0.306397,"max":15.247754,"p(90)":0.34756159999999997,"p(95)":0.36963129999999983}},"http_req_connecting":{"type":"trend","contains":"time","values":{"p(90)":0,"p(95)":0,"avg":0.00004366758091058694,"min":0,"med":0,"max":0.169157}},"data_received":{"type":"counter","contains":"data","values":{"count":2915926,"rate":962724.4238484775}},"checks":{"type":"rate","contains":"default","values":{"rate":1,"passes":6,"fails":0}},"http_req_sending":{"type":"trend","contains":"time","values":{"max":0.070261,"p(90)":0.010546,"p(95)":0.011454,"avg":0.008327501151947327,"min":0.004679,"med":0.008031}},"vus_max":{"type":"gauge","contains":"default","values":{"value":1,"min":1,"max":1}},"http_req_tls_handshaking":{"type":"trend","contains":"time","values":{"med":0,"max":0,"p(90)":0,"p(95)":0,"avg":0,"min":0}},"vus":{"type":"gauge","contains":"default","values":{"value":1,"min":1,"max":1}},"http_req_duration":{"type":"trend","contains":"time","values":{"avg":0.2788634223806909,"min":0.219933,"med":0.266726,"max":5.222222,"p(90)":0.30291900000000005,"p(95)":0.32406630000000003}},"http_req_receiving":{"type":"trend","contains":"time","values":{"min":0.012781,"med":0.02619,"max":0.367788,"p(90)":0.031429,"p(95)":0.033384,"avg":0.02639306132748222}},"data_sent":{"type":"counter","contains":"data","values":{"count":802845,"rate":265067.93727434473}},"http_req_blocked":{"values":{"min":0.002793,"med":0.004679,"max":0.306257,"p(90)":0.005238,"p(95)":0.005587,"avg":0.004666356555128879},"type":"trend","contains":"time"},"http_reqs":{"contains":"default","values":{"count":9115,"rate":3009.4155761767865},"type":"counter"}}}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/go-playground/validator/v10 v10.14.1
github.com/google/uuid v1.3.0
github.com/mitchellh/mapstructure v1.5.0
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/shopspring/decimal v1.3.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand Down
18 changes: 18 additions & 0 deletions pkg/detectors/attributes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package detectors

import (
"database/sql"
"encoding"
"fmt"
"reflect"

"github.com/glothriel/grf/pkg/fields"
Expand Down Expand Up @@ -40,16 +42,28 @@ type fieldSettings struct {

isGRFRepresentable bool
isGRFParsable bool
isForeignKey bool

isSqlNullInt32 bool
}

func getFieldSettings[Model any](fieldName string) *fieldSettings {
var entity Model
var settings *fieldSettings
fieldTypes := FieldTypes[Model]()
for _, field := range reflect.VisibleFields(reflect.TypeOf(entity)) {
jsonTag := field.Tag.Get("json")
if jsonTag == fieldName {
var theTypeAsAny any
reflectedInstance := reflect.New(reflect.TypeOf(reflect.ValueOf(entity).FieldByName(field.Name).Interface())).Elem()

isForeignKey := false
if _, ok := fieldTypes[fmt.Sprintf(
"%s_id", jsonTag,
)]; ok {
isForeignKey = true
}

if reflectedInstance.CanAddr() {
theTypeAsAny = reflectedInstance.Addr().Interface()
} else {
Expand All @@ -60,6 +74,7 @@ func getFieldSettings[Model any](fieldName string) *fieldSettings {
_, isEncodingTextUnmarshaler := theTypeAsAny.(encoding.TextUnmarshaler)
_, isGRFRepresentable := theTypeAsAny.(fields.GRFRepresentable)
_, isGRFParsable := theTypeAsAny.(fields.GRFParsable)
_, isSqlNull32 := theTypeAsAny.(*sql.NullInt32)

settings = &fieldSettings{
itsType: reflect.TypeOf(
Expand All @@ -69,6 +84,9 @@ func getFieldSettings[Model any](fieldName string) *fieldSettings {
isEncodingTextUnmarshaler: isEncodingTextUnmarshaler,
isGRFRepresentable: isGRFRepresentable,
isGRFParsable: isGRFParsable,
isForeignKey: isForeignKey,

isSqlNullInt32: isSqlNull32,
}
}
}
Expand Down
Loading

0 comments on commit 24a0936

Please sign in to comment.