Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
c7df9d2264
|
|||
d0954bf133
|
|||
8affa81bb9
|
|||
fe9ebf0bab
|
|||
a4b5f33d15
|
|||
e89e2c18f2
|
|||
b16d5152c7
|
|||
5fb2f8a312
|
6
.idea/goext.iml
generated
6
.idea/goext.iml
generated
@@ -1,6 +1,10 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<module type="WEB_MODULE" version="4">
|
<module type="WEB_MODULE" version="4">
|
||||||
<component name="Go" enabled="true" />
|
<component name="Go" enabled="true">
|
||||||
|
<buildTags>
|
||||||
|
<option name="goVersion" value="1.19" />
|
||||||
|
</buildTags>
|
||||||
|
</component>
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$" />
|
<content url="file://$MODULE_DIR$" />
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
@@ -32,6 +32,8 @@ echo "> Current Version: ${curr_vers}"
|
|||||||
echo "> Next Version: ${next_ver}"
|
echo "> Next Version: ${next_ver}"
|
||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
|
printf "package goext\n\nconst GoextVersion = \"%s\"\n\nconst GoextVersionTimestamp = \"%s\"\n" "${next_ver}" "$( date +"%Y-%m-%dT%H:%M:%S%z" )" > "goextVersion.go"
|
||||||
|
|
||||||
git add --verbose .
|
git add --verbose .
|
||||||
|
|
||||||
msg="v${next_ver}"
|
msg="v${next_ver}"
|
||||||
|
@@ -3,11 +3,14 @@ package bfcodegen
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/cmdext"
|
"gogs.mikescher.com/BlackForestBytes/goext/cmdext"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/cryptext"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/rext"
|
"gogs.mikescher.com/BlackForestBytes/goext/rext"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -32,6 +35,8 @@ var rexEnumDef = rext.W(regexp.MustCompile("^\\s*type\\s+(?P<name>[A-Za-z0-9_]+)
|
|||||||
|
|
||||||
var rexValueDef = rext.W(regexp.MustCompile("^\\s*(?P<name>[A-Za-z0-9_]+)\\s+(?P<type>[A-Za-z0-9_]+)\\s*=\\s*(?P<value>(\"[A-Za-z0-9_:]+\"|[0-9]+))\\s*(//(?P<descr>.*))?.*$"))
|
var rexValueDef = rext.W(regexp.MustCompile("^\\s*(?P<name>[A-Za-z0-9_]+)\\s+(?P<type>[A-Za-z0-9_]+)\\s*=\\s*(?P<value>(\"[A-Za-z0-9_:]+\"|[0-9]+))\\s*(//(?P<descr>.*))?.*$"))
|
||||||
|
|
||||||
|
var rexChecksumConst = rext.W(regexp.MustCompile("const ChecksumGenerator = \"(?P<cs>[A-Za-z0-9_]*)\""))
|
||||||
|
|
||||||
func GenerateEnumSpecs(sourceDir string, destFile string) error {
|
func GenerateEnumSpecs(sourceDir string, destFile string) error {
|
||||||
|
|
||||||
files, err := os.ReadDir(sourceDir)
|
files, err := os.ReadDir(sourceDir)
|
||||||
@@ -39,17 +44,46 @@ func GenerateEnumSpecs(sourceDir string, destFile string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldChecksum := "N/A"
|
||||||
|
if _, err := os.Stat(destFile); !os.IsNotExist(err) {
|
||||||
|
content, err := os.ReadFile(destFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if m, ok := rexChecksumConst.MatchFirst(string(content)); ok {
|
||||||
|
oldChecksum = m.GroupByName("cs").Value()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
files = langext.ArrFilter(files, func(v os.DirEntry) bool { return v.Name() != path.Base(destFile) })
|
||||||
|
files = langext.ArrFilter(files, func(v os.DirEntry) bool { return strings.HasSuffix(v.Name(), ".go") })
|
||||||
|
langext.SortBy(files, func(v os.DirEntry) string { return v.Name() })
|
||||||
|
|
||||||
|
newChecksumStr := goext.GoextVersion
|
||||||
|
for _, f := range files {
|
||||||
|
content, err := os.ReadFile(path.Join(sourceDir, f.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newChecksumStr += "\n" + f.Name() + "\t" + cryptext.BytesSha256(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
newChecksum := cryptext.BytesSha256([]byte(newChecksumStr))
|
||||||
|
|
||||||
|
if newChecksum != oldChecksum {
|
||||||
|
fmt.Printf("[EnumGenerate] Checksum has changed ( %s -> %s ), will generate new file\n\n", oldChecksum, newChecksum)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("[EnumGenerate] Checksum unchanged ( %s ), nothing to do\n", oldChecksum)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
allEnums := make([]EnumDef, 0)
|
allEnums := make([]EnumDef, 0)
|
||||||
|
|
||||||
pkgname := ""
|
pkgname := ""
|
||||||
|
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
if !strings.HasSuffix(f.Name(), ".go") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("========= %s =========\n\n", f.Name())
|
fmt.Printf("========= %s =========\n\n", f.Name())
|
||||||
fileEnums, pn, err := processFile(f.Name())
|
fileEnums, pn, err := processFile(path.Join(sourceDir, f.Name()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -67,7 +101,7 @@ func GenerateEnumSpecs(sourceDir string, destFile string) error {
|
|||||||
return errors.New("no package name found in any file")
|
return errors.New("no package name found in any file")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile(destFile, []byte(fmtOutput(allEnums, pkgname)), 0o755)
|
err = os.WriteFile(destFile, []byte(fmtOutput(newChecksum, allEnums, pkgname)), 0o755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -159,8 +193,8 @@ func processFile(fn string) ([]EnumDef, string, error) {
|
|||||||
return enums, pkgname, nil
|
return enums, pkgname, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fmtOutput(enums []EnumDef, pkgname string) string {
|
func fmtOutput(cs string, enums []EnumDef, pkgname string) string {
|
||||||
str := "// Code generated by permissions_gen.sh DO NOT EDIT.\n"
|
str := "// Code generated by enum-generate.go DO NOT EDIT.\n"
|
||||||
str += "\n"
|
str += "\n"
|
||||||
str += "package " + pkgname + "\n"
|
str += "package " + pkgname + "\n"
|
||||||
str += "\n"
|
str += "\n"
|
||||||
@@ -168,6 +202,9 @@ func fmtOutput(enums []EnumDef, pkgname string) string {
|
|||||||
str += "import \"gogs.mikescher.com/BlackForestBytes/goext/langext\"" + "\n"
|
str += "import \"gogs.mikescher.com/BlackForestBytes/goext/langext\"" + "\n"
|
||||||
str += "\n"
|
str += "\n"
|
||||||
|
|
||||||
|
str += "const ChecksumGenerator = \"" + cs + "\"" + "\n"
|
||||||
|
str += "\n"
|
||||||
|
|
||||||
str += "type Enum interface {" + "\n"
|
str += "type Enum interface {" + "\n"
|
||||||
str += " Valid() bool" + "\n"
|
str += " Valid() bool" + "\n"
|
||||||
str += " ValuesAny() []any" + "\n"
|
str += " ValuesAny() []any" + "\n"
|
||||||
|
15
bfcodegen/enum-generate_test.go
Normal file
15
bfcodegen/enum-generate_test.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package bfcodegen
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestApplyEnvOverridesSimple(t *testing.T) {
|
||||||
|
|
||||||
|
err := GenerateEnumSpecs("/home/mike/Code/reiff/badennet/bnet-backend/models", "/home/mike/Code/reiff/badennet/bnet-backend/models/enums_gen.go")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -6,5 +6,5 @@ import (
|
|||||||
|
|
||||||
type Filter interface {
|
type Filter interface {
|
||||||
FilterQuery() mongo.Pipeline
|
FilterQuery() mongo.Pipeline
|
||||||
Pagination() (string, SortDirection, *string, *SortDirection)
|
Pagination() (string, SortDirection, string, SortDirection)
|
||||||
}
|
}
|
||||||
|
5
goextVersion.go
Normal file
5
goextVersion.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package goext
|
||||||
|
|
||||||
|
const GoextVersion = "0.0.146"
|
||||||
|
|
||||||
|
const GoextVersionTimestamp = "2023-06-07T12:59:15+0200"
|
@@ -57,8 +57,21 @@ func CreatePagination[TData any](coll *Coll[TData], token ct.CursorToken, fieldP
|
|||||||
|
|
||||||
pipeline := make([]bson.D, 0, 3)
|
pipeline := make([]bson.D, 0, 3)
|
||||||
|
|
||||||
|
if token.Mode == ct.CTMStart {
|
||||||
|
|
||||||
|
// no gt/lt condition
|
||||||
|
|
||||||
|
} else if token.Mode == ct.CTMNormal {
|
||||||
|
|
||||||
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.M{"$or": cond}}})
|
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.M{"$or": cond}}})
|
||||||
|
|
||||||
|
} else if token.Mode == ct.CTMEnd {
|
||||||
|
|
||||||
|
// false
|
||||||
|
pipeline = append(pipeline, bson.D{{Key: "$match", Value: bson.M{"$eq": bson.A{"1", "0"}}}})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
pipeline = append(pipeline, bson.D{{Key: "$sort", Value: sort}})
|
pipeline = append(pipeline, bson.D{{Key: "$sort", Value: sort}})
|
||||||
|
|
||||||
if pageSize != nil {
|
if pageSize != nil {
|
||||||
|
@@ -1,5 +1,11 @@
|
|||||||
package wmo
|
package wmo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
)
|
||||||
|
|
||||||
func (c *Coll[TData]) DeleteOne(ctx context.Context, id EntityID) error {
|
func (c *Coll[TData]) DeleteOne(ctx context.Context, id EntityID) error {
|
||||||
_, err := c.coll.DeleteOne(ctx, bson.M{"_id": id})
|
_, err := c.coll.DeleteOne(ctx, bson.M{"_id": id})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -13,7 +13,17 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int,
|
|||||||
|
|
||||||
pipeline := filter.FilterQuery()
|
pipeline := filter.FilterQuery()
|
||||||
|
|
||||||
sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary := filter.Pagination()
|
pf1, pd1, pf2, pd2 := filter.Pagination()
|
||||||
|
|
||||||
|
sortPrimary := pf1
|
||||||
|
sortDirPrimary := pd1
|
||||||
|
sortSecondary := &pf2
|
||||||
|
sortDirSecondary := &pd2
|
||||||
|
|
||||||
|
if pf1 == pf2 {
|
||||||
|
sortSecondary = nil
|
||||||
|
sortDirSecondary = nil
|
||||||
|
}
|
||||||
|
|
||||||
paginationPipeline, err := CreatePagination(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize)
|
paginationPipeline, err := CreatePagination(c, inTok, sortPrimary, sortDirPrimary, sortSecondary, sortDirSecondary, pageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -48,6 +58,10 @@ func (c *Coll[TData]) List(ctx context.Context, filter ct.Filter, pageSize *int,
|
|||||||
return entities, nextToken, nil
|
return entities, nextToken, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type countRes struct {
|
||||||
|
Count int64 `bson:"c"`
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error) {
|
func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error) {
|
||||||
pipeline := filter.FilterQuery()
|
pipeline := filter.FilterQuery()
|
||||||
|
|
||||||
@@ -58,12 +72,8 @@ func (c *Coll[TData]) Count(ctx context.Context, filter ct.Filter) (int64, error
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type res struct {
|
|
||||||
Count int64 `bson:"c"`
|
|
||||||
}
|
|
||||||
|
|
||||||
if cursor.Next(ctx) {
|
if cursor.Next(ctx) {
|
||||||
v := res{}
|
v := countRes{}
|
||||||
err = cursor.Decode(&v)
|
err = cursor.Decode(&v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@@ -3,6 +3,7 @@ package wmo
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,6 +36,15 @@ func (c *Coll[TData]) UpdateOneByID(ctx context.Context, id EntityID, updateQuer
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Coll[TData]) UpdateMany(ctx context.Context, filterQuery bson.M, updateQuery bson.M) (*mongo.UpdateResult, error) {
|
||||||
|
res, err := c.coll.UpdateMany(ctx, filterQuery, updateQuery)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error {
|
func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData) error {
|
||||||
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value)
|
_, err := c.coll.UpdateOne(ctx, bson.M{"_id": id}, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -43,3 +53,14 @@ func (c *Coll[TData]) ReplaceOne(ctx context.Context, id EntityID, value TData)
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Coll[TData]) FindOneAndReplace(ctx context.Context, id EntityID, value TData) (TData, error) {
|
||||||
|
var res TData
|
||||||
|
|
||||||
|
err := c.coll.FindOneAndUpdate(ctx, bson.M{"_id": id}, value, options.FindOneAndUpdate().SetReturnDocument(options.After)).Decode(&res)
|
||||||
|
if err != nil {
|
||||||
|
return *new(TData), err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user