sheetdb-modeler

command
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2019 License: MIT Imports: 16 Imported by: 0

README

sheetdb-modeler

A tool that automatically generates CRUD functions based on the defined model, Go structure.

Installation

go get github.com/takuoki/sheetdb/tools/sheetdb-modeler

How to write models

Field type

The following types and their pointer types are available as fields of models:

  • string
  • bool
  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • sheetdb.Date, sheetdb.Datetime

Define nullable fields with pointer type. However, the pointer type of string can not be used.

You can use user-defined types. In this case, create NewTypeName function and String method at the same time.

type Sex int

func NewSex(sex string) (Sex, error) {
  // ...
}

func (s *Sex) String() string {
  if s == nil {
    return ""
  }
  // ...
}

Currently, user-defined types in different packages are not available.

Primary key

Specify the primary key field using the annotation tag primarykey. In models without a parent model, the number of primary key fields is always one. For child models, the number of primary key fields is the number of parent models plus one (composite primary key). The key name, type and order must match the order of the parent model. Note that you can not use pointer type, string type with allowempty or unique tag as primary key type.

Auto numbering of ID

A primary key which field name ends with "ID" and type is int is automatically numbered. This field is not specified as an argument of AddModel function.

The rules for automatic numbering are as follows.

  • Models without a parent model

    The automatically numbered ID is Line number on Spreadsheet + Numbering initial value - 1. Even if data is deleted, IDs is not duplicated.

  • Child Models

    The automatically assignumberedned ID is the maximum value of IDs in the same parent model + 1. Numbering initial value if there is no data in the same parent model. There may be duplication with deleted data.

Constraints
Non-null constraint

If the field type is string, pointer type is not allowed, but it may contain empty string. However, empty string is not allowed unless you explicitly write the allowempty tag.

If the field type is other than string, defining it as a pointer type makes the field nullable. If it is not defined as a pointer type, it can not be null according to Go's language specification.

Unique constraint

If there is a field that you want to secure uniqueness other than the primary key, write a unique tag. Fields with the unique tag are unique in the same parent model, if they are child models. Currently only available for string type fields.

How to generate models

It is automatically generated by sheetdb-modeler command. You can generate in bulk with the go generate command by putting comments in the code of the target package.

//go:generate sheetdb-modeler -type=User -children=Foo,FooChild,Bar -initial=10001
go generate ./sample

The options are as follows.

  • -type

    Type of structure to be generated automatically. Required.

  • -parent, -children

    Specify if parent model and child model exist. Specify multiple child models separated by commas. Beware of deadlocks as they are locked in the order specified by the child model.

  • -initial

    Set the initial value of automatic numbering. It can not be specified if the primary key is not numbered automatically.

  • -private

    If you want to customize the interface for an external package, output the code with private mode and wrap them.

  • -client, -modelset

    You can split data into multiple spreadsheets by using the client, and modelSet option. However, models in parent-child relationships can not be divided into separate sheets.

  • -output

    A file is created for each model (type), and you can specify a file name with the -output option. The default is model_typename.go.

  • -test

    For testing, it is an option to automatically generate code without writing only to a spreadsheet. If the value is "on", it will be output in test mode. Please refer to the FAQ for more details.

FAQ

Why do I get compilation errors?

Since commands are executed separately for each model, it is not possible to detect contradictions in option specification between models at the time of automatic generation. So we detect these contradictions with compilation errors in the _ function. If there is a compilation error in this function, please check the comment and fix it.

Note that there is a possibility of a bug in the tool if the automatic generation is successful but there are compilation errors in other than the _ function.

What should I take care of for automated testing?

If the values ​​in the spreadsheet are updated each time you run an automated test, it will be difficult to run the test under the same conditions each time. You can avoid these problems by using auto-generated code with test mode when testing. If you do not want to destroy test data due to accidentally running a test in code not generated with test mode, you can make it a compile error by referring to the constant, like TestMode_ModelName, that is output only in test mode.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL