Skip to content

Instantly share code, notes, and snippets.

@tauraamui
tauraamui / runtime.c.v
Created April 4, 2024 15:02
Using LuaJIT library with V to load tables from Lua into structs
module luajit
#flag -I/usr/local/include/luajit-2.1
#flag -L/usr/local/lib
#flag -lluajit-5.1
#flag -lm
#include "lua.h"
#include "lauxlib.h"
pub struct C.lua_State {}
// Internal growinh procedure
_grow :: proc(q: ^$Q/Queue($T), min_capacity: uint = 0) -> bool {
new_capacity := max(min_capacity, uint(8), uint(builtin.len(q.data))*2)
n := uint(builtin.len(q.data))
builtin.resize(&q.data, int(new_capacity)) or_return
if q.offset + q.len > n {
diff := n - q.offset
copy(q.data[new_capacity-diff:], q.data[q.offset:][:diff])
q.offset += new_capacity - n
}
@tauraamui
tauraamui / behaviourtest.go
Last active July 7, 2021 06:35
Dragon Daemon re-connect logic with test
It("Should attempt to reconnect until read eventually returns ok", func() {
infoLogs := []string{}
resetLogInfo := media.OverloadLogInfo(func(format string, args ...interface{}) {
// fmt.Printf("INFO LOG FORMAT: %s\n", format)
infoLogs = append(infoLogs, fmt.Sprintf(format, args))
})
defer resetLogInfo()
processCallCount := 0
ableToRead := true
@tauraamui
tauraamui / makelogginglibmockable.go
Last active July 4, 2021 13:19
Three different approaches to making it possible to cover logging outputs in code tests
// VERSION 1
err := c.cache.Set(sizeOnDiskCacheKey, []byte(sizeStrWithUnitSuffix))
if err != nil {
// impossible to mock as logging.Error is unoverridable func def
logging.Error("unable to store disk size in cache: %w", err)
}
// VERSION 2
// define local overridable log error wrapper
@tauraamui
tauraamui / concurrent-recursive-file-size-counter.go
Created March 2, 2021 09:33
Go code to concurrently recursively walk file tree counting files, spawning new Goroutine per sub directory found.
// Entry point call, pass in valid directory path and nil file pointer
total, err := getDirSize("/root/dir/to/count/from", nil)
// will either get empty string and pointer or filled string with nil pointer
// depending on whether it needs to just count the files still remaining in
// this given dir or whether it needs to start counting again in a found sub dir
func getDirSize(path string, filePtr *os.File) (int64, error) {
var total int64
@tauraamui
tauraamui / nosql_contact.go
Created October 10, 2019 10:53
Golang SQL vs NOSQL usage
// This function uses the generic store any data structure here
// the downside of using NoSQL is there isn't any enforced inherent
// relationship between the data, but long term cloud software dev
// had made the industry realise that this isn't actually an advantage
// most of the time. Cascading deletes? Who cares right?
// The store implementation used can be switched via cmd line flag to
// use either MongoDB or Google Cloud Datastore, so this impl. just
// checks which one is in use and stores the ID of the correct type.
@tauraamui
tauraamui / gendbexample.go
Created September 12, 2019 08:00
To show how to potentially populate a slice of interface from MongoDB and GCS
"store.go"
type Store interface {
Save(ctx context.Context, model interface{}) (interface{}, error)
FindAll(ctx context.Context, key string, value string, dst interface{}) error
}
type MongoDBStore struct {
db *mongo.Collection
}
@tauraamui
tauraamui / iframe-inside-bootstrap-3-modal.markdown
Created January 30, 2019 15:14
iFrame inside Bootstrap 3 modal

Keybase proof

I hereby claim:

  • I am tauraamui on github.
  • I am tauraamui (https://keybase.io/tauraamui) on keybase.
  • I have a public key ASCsS7gEZEmeE18hfsQN38JZ6uOI0HdQlWZS6sgxAI1qowo

To claim this, I am signing this object: