Skip to content

Commit

Permalink
refactor: Remove the ignorefields example and use simple unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
daviderli614 committed Dec 13, 2024
1 parent ca6389e commit 0e645ae
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 420 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ If you prefer ORM style, and define column-field relationship via struct field t
- `type` is to define the data type. if type is timestamp, `precision` is supported
- the metadata separator is `;` and the key value separator is `:`

type supported is the same as described [Datatypes supported](#datatypes-supported), and case insensitive
type supported is the same as described [Datatypes supported](#datatypes-supported), and case insensitive.

When fields marked with `greptime:"-"`, writing field will be ignored.

##### define struct with tags

Expand Down
1 change: 0 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ docker run --rm -p 4000-4003:4000-4003 \
- [opentelemetry](opentelemetry/README.md)
- [hints](hints/README.md)
- [jsondata](jsondata/README.md)
- [skipfields](skipfields/README.md)

## Query

Expand Down
45 changes: 0 additions & 45 deletions examples/skipfields/README.md

This file was deleted.

131 changes: 0 additions & 131 deletions examples/skipfields/main.go

This file was deleted.

4 changes: 2 additions & 2 deletions schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newColumnSchema(columnName string, semanticType gpb.SemanticType, datatype
func parseField(structField reflect.StructField) (*Field, error) {
tags := parseTag(structField)

if _, ok := tags["-"]; ok && len(tags) == 1 {
if _, ok := tags[IgnoreFiledTag]; ok && len(tags) == 1 {
return nil, nil
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func parseField(structField reflect.StructField) (*Field, error) {
func parseTag(structField reflect.StructField) map[string]string {
tags := map[string]string{}

str, ok := structField.Tag.Lookup("greptime")
str, ok := structField.Tag.Lookup(GreptimeFieldTagKey)
if !ok {
return tags
}
Expand Down
15 changes: 10 additions & 5 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (
"github.com/GreptimeTeam/greptimedb-ingester-go/util"
)

const (
IgnoreFiledTag = "-"
GreptimeFieldTagKey = "greptime"
)

type Schema struct {
tableName string

Expand Down Expand Up @@ -145,17 +150,17 @@ func (s *Schema) parseValues(input any) error {
}

visibleFields := reflect.VisibleFields(typ)
size := make([]reflect.StructField, 0, len(visibleFields))
values := make([]*gpb.Value, 0, len(size))
processingFields := make([]reflect.StructField, 0, len(visibleFields))
values := make([]*gpb.Value, 0, len(processingFields))

for _, structField := range visibleFields {
if !structField.IsExported() || structField.Tag.Get("greptime") == "-" {
if !structField.IsExported() || structField.Tag.Get(GreptimeFieldTagKey) == IgnoreFiledTag {
continue
}
size = append(size, structField)
processingFields = append(processingFields, structField)
}

for i, structField := range size {
for i, structField := range processingFields {
field := s.fields[i]
value, err := parseValue(field.Datatype, val.FieldByName(structField.Name))
if err != nil {
Expand Down
Loading

0 comments on commit 0e645ae

Please sign in to comment.