-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add picker to pick value from record #246
Merged
+1,477
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package picker | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Config is the configuration to build Picker | ||
// The priority is as follows: | ||
// ConcatItems > Indices | ||
// Nullable | ||
// DefaultValue | ||
// NullValue, if set to null, subsequent conversions will be skipped. | ||
// Type | ||
// Function | ||
// CheckOnPost | ||
type Config struct { | ||
ConcatItems ConcatItems // Concat index column, constant, or mixed. | ||
Indices []int // Set index columns, the first non-null. | ||
Nullable func(string) bool // Determine whether it is null. Optional. | ||
NullValue string // Set null value when it is null. Optional. | ||
DefaultValue *string // Set default value when it is null. Optional. | ||
Type string // Set the type of value. | ||
Function *string // Set the conversion function of value. | ||
CheckOnPost func(*Value) error // Set the value check function on post. | ||
} | ||
|
||
func (c *Config) Build() (Picker, error) { | ||
var retPicker Picker | ||
var nullHandled bool | ||
switch { | ||
case c.ConcatItems.Len() > 0: | ||
retPicker = ConcatPicker{ | ||
items: c.ConcatItems, | ||
} | ||
case len(c.Indices) == 1: | ||
retPicker = IndexPicker(c.Indices[0]) | ||
case len(c.Indices) > 1: | ||
if c.Nullable == nil { | ||
// the first must be picked | ||
retPicker = IndexPicker(c.Indices[0]) | ||
} else { | ||
pickers := make(NullablePickers, 0, len(c.Indices)) | ||
for _, index := range c.Indices { | ||
pickers = append(pickers, ConverterPicker{ | ||
picker: IndexPicker(index), | ||
converter: NullableConverters{ | ||
NullableConverter{ | ||
Nullable: c.Nullable, | ||
}, | ||
}, | ||
}) | ||
} | ||
retPicker = pickers | ||
} | ||
nullHandled = true | ||
default: | ||
return nil, fmt.Errorf("no indices or concat items") | ||
} | ||
|
||
var converters []Converter | ||
|
||
if !nullHandled && c.Nullable != nil { | ||
converters = append(converters, NullableConverter{ | ||
Nullable: c.Nullable, | ||
}) | ||
} | ||
|
||
if c.Nullable != nil { | ||
if c.DefaultValue != nil { | ||
converters = append(converters, DefaultConverter{ | ||
Value: *c.DefaultValue, | ||
}) | ||
} else { | ||
converters = append(converters, NullConverter{ | ||
Value: c.NullValue, | ||
}) | ||
} | ||
} | ||
|
||
converters = append(converters, NewTypeConverter(c.Type)) | ||
|
||
if c.Function != nil && *c.Function != "" { | ||
var functionConverter Converter = FunctionConverter{ | ||
Name: *c.Function, | ||
} | ||
if strings.EqualFold(*c.Function, "hash") && !strings.EqualFold(c.Type, "string") { | ||
functionConverter = FunctionStringConverter{ | ||
Name: *c.Function, | ||
} | ||
} | ||
converters = append(converters, functionConverter) | ||
} | ||
|
||
if c.CheckOnPost != nil { | ||
converters = append(converters, ConverterFunc(func(v *Value) (*Value, error) { | ||
if err := c.CheckOnPost(v); err != nil { | ||
return nil, err | ||
} | ||
return v, nil | ||
})) | ||
} | ||
|
||
var converter Converter = Converters(converters) | ||
if c.Nullable != nil { | ||
converter = NullableConverters(converters) | ||
} | ||
|
||
return ConverterPicker{ | ||
picker: retPicker, | ||
converter: converter, | ||
}, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can combine them ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌🏻