4/29/2023 0 Comments Golang uuid generatorNewtodo/todo_generated_api.go: //Auto generated with MetaApi package main import ( "database/sql" _ "/lib/pq" "time" ) //Create Table func CreateTableTodos(db *sql.DB) (err error) fmt. Newtodo/todo.sql: create table todos ( id integer generated always as identity primary key, updated_at timestamptz, done boolean, title text ) We will just need to create a new template for our test file, some new receiver methods in generate.go, and integrate these things.Īs a refresher, let’s look first at our automatically generated metaapi api that we will be testing against, based on a simple todo sql table definition. We can leave all of the original lexer and parser untouched. That’s how it should be - it makes sense that if you can metaprogram some thing x.go you should also be able to metaprogram some thing x_test.go without too much additional effort. It differs from these earlier packages in that a UUID is a 16 byte array rather than a byte slice. This package is based on the /pborman/uuid package (previously named /p/go-uuid). Fortunately, our metaapi program gives us almost all we need as a springboard to make this easy. uuid The uuid package generates and inspects UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services. Icing on the cake: why force users to write tests manually if you can automatically generate them?Īrmed with these arguments let’s make some automatic tests in go.Automatic tests are a test of the generator itself and so should be implemented as a process of building the generator (my bad, and in fact I did find problems with the original metaapi generated code for some of the sql types I do not often use).The context (other code) that the generated code finds itself in may exercise it in ways that the generator author never considered.The user may modify/extend the generated code over time and will need tests when the code changes.The automatic tests become a contract and document of the extent to which the generated code has been tested.Moreover, the generator writer is as obliged to provide automatic tests as if they were writing non-meta (normal?) programs. Of course, you cannot assume that generated code is bug-free for the simple reason that generated code is a product of a generator, and the generator itself is still very subject to human error. Here is a question though - should you build tests for automatically generated code? You might assume that if the generator is working correctly, then it is generating bug-free code. One thing missing from many projects is testing the code. We’ll discuss all of these in more detail. Automatically create an initial metaapi project.Support code generation outside of metaapi.Use internal templates for default code generation.Automatically create tests for our metaapi generated api.Let’s set four goals for improving on metaapi. What more can we metaprogram? A lot, it turns out, but everything takes time. Along the way, we’ll also build a couple of utilities that will make our metaprogramming tool more useful.įlushed with the excitement of automatically creating 100's of lines of compilable, runnable, bug-free* (* not really, keep reading) code from just a few lines of SQL table definition we are like hammers, looking for a nail. Recall that we are already automatically making the api themselves based on PostgreSQL table definitions. My current python code is: uuid.uuid1(). In this article, we will focus on automatically creating tests for our CRUD api. I need to generate an unique file name with UUID1. This is a follow-on to the last article, Metaprogram in Go, probably start there if you have not read it yet. In which, we build on our Golang metaprogramming tool. The gist for this code can be found here.įor more articles like this, consider following my personal website.Further adventures with metaprogramming in Go. Base contains common columns for all tables. This is the same structure as GORM’s Model base class, but with the ID type swapped to UUID.įeel free to skip this if you don’t care about the time fields, and embed the ID field directly. Next, we will create a Base model struct that our subsequent models will embed to contain common columns, mainly: id, created_at, updated_at, and deleted_at. Go get /jinzhu/gorm go get /satori/go.uuid Base Model Installationįirst, you’ll need to install the appropriate packages: In this article, I’ll run through a quick example using UUID as the key type for some tables. And while the documentation is also quite good, I couldn’t find a good example for a specific use case I ran into: using UUID s as keys. It has covered all of my ORM related needs. On the rare occasion I’ve needed to use an external framework, I’ve found the community support to be just as thorough.Īmong the more useful packages I’ve come across is GORM, a fantastic ORM for the Go language. One of the best features is the incredibly deep set of packages that is part of the standard library. I recently picked up Go after years of Python, Java, and most recently, Kotlin.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |