go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

goctl model parser, Field struct to have the name as declared in the model type

Open yogibaba opened this issue 1 year ago • 2 comments

The field struct which has the NameOriginal, Name, DataType, Comment, SeqInIndex, OrdinalPosition, should also have the model type name so as to identify or link between the model type and field.

	Field struct {
		NameOriginal    string
		Name            stringx.String
		StructField     string
		DataType        string
		Comment         string
		SeqInIndex      int
		OrdinalPosition int
	}

A sample generated model. eg:

	type Student struct {
		ID         int64           `db:"id"`
		Name       string          `db:"name"`
		Age        sql.NullInt64   `db:"age"`
		Score      sql.NullFloat64 `db:"score"`
		CreateTime time.Time       `db:"create_time"`
		UpdateTime sql.NullTime    `db:"update_time"`
	}

The field name should be available while passing the data (parser.Table) object to the template.

A sample generated update function which makes use of the model type to generate the squirrel builder. eg:

	query, values, err := squirrel.
		Update(m.table).
		SetMap(squirrel.Eq{
			name:        data.Name,
			age:         data.Age,
			score:       data.Score,
			update_time: data.UpdateTime,
		}).
		Where(squirrel.Eq{"id": data.ID}).
		ToSql()

yogibaba avatar Jul 06 '22 14:07 yogibaba

What's the problem can be resolved? I saw it add a came case field, it's not need to do this action, you can use field Name and call stringx package instead.

kesonan avatar Jul 09 '22 14:07 kesonan

I utilized the struct Field.Name as it is of type stringx, but the resultant varied for some columns as, SafeString has been used to convert the tableName, columnName, modelFileName after converting to camel case.

If SafeString also would have been the available in stringx there was no need to use camel-case and then safe-string in the modified files, or making utils available as list of functions to all the templates would be useful.

yogibaba avatar Jul 11 '22 03:07 yogibaba