mirror of
https://github.com/go-task/task.git
synced 2025-05-15 22:26:28 +02:00
fix: deep copying pointers inside slices (#1072)
This commit is contained in:
parent
d72eb009e4
commit
cc1fd3d03e
@ -15,6 +15,7 @@
|
|||||||
arguments not whitespaces
|
arguments not whitespaces
|
||||||
([#1040](https://github.com/go-task/task/issues/1040), [#1059](https://github.com/go-task/task/pull/1059) by @dhanusaputra).
|
([#1040](https://github.com/go-task/task/issues/1040), [#1059](https://github.com/go-task/task/pull/1059) by @dhanusaputra).
|
||||||
- Fix the value of `{{.CHECKSUM}}` variable in status ([#1076](https://github.com/go-task/task/issues/1076), [#1080](https://github.com/go-task/task/pull/1080) by @pd93).
|
- Fix the value of `{{.CHECKSUM}}` variable in status ([#1076](https://github.com/go-task/task/issues/1076), [#1080](https://github.com/go-task/task/pull/1080) by @pd93).
|
||||||
|
- Fixed deep copy implementation ([#1072](https://github.com/go-task/task/pull/1072) by @pd93)
|
||||||
|
|
||||||
## v3.22.0 - 2023-03-10
|
## v3.22.0 - 2023-03-10
|
||||||
|
|
||||||
|
@ -19,10 +19,21 @@ type Cmd struct {
|
|||||||
Platforms []*Platform
|
Platforms []*Platform
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dep is a task dependency
|
func (c *Cmd) DeepCopy() *Cmd {
|
||||||
type Dep struct {
|
if c == nil {
|
||||||
Task string
|
return nil
|
||||||
Vars *Vars
|
}
|
||||||
|
return &Cmd{
|
||||||
|
Cmd: c.Cmd,
|
||||||
|
Silent: c.Silent,
|
||||||
|
Task: c.Task,
|
||||||
|
Set: deepCopySlice(c.Set),
|
||||||
|
Shopt: deepCopySlice(c.Shopt),
|
||||||
|
Vars: c.Vars.DeepCopy(),
|
||||||
|
IgnoreError: c.IgnoreError,
|
||||||
|
Defer: c.Defer,
|
||||||
|
Platforms: deepCopySlice(c.Platforms),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cmd) UnmarshalYAML(node *yaml.Node) error {
|
func (c *Cmd) UnmarshalYAML(node *yaml.Node) error {
|
||||||
@ -94,30 +105,3 @@ func (c *Cmd) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
|
|
||||||
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into command", node.Line, node.ShortTag())
|
return fmt.Errorf("yaml: line %d: cannot unmarshal %s into command", node.Line, node.ShortTag())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dep) UnmarshalYAML(node *yaml.Node) error {
|
|
||||||
switch node.Kind {
|
|
||||||
|
|
||||||
case yaml.ScalarNode:
|
|
||||||
var task string
|
|
||||||
if err := node.Decode(&task); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
d.Task = task
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case yaml.MappingNode:
|
|
||||||
var taskCall struct {
|
|
||||||
Task string
|
|
||||||
Vars *Vars
|
|
||||||
}
|
|
||||||
if err := node.Decode(&taskCall); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
d.Task = taskCall.Task
|
|
||||||
d.Vars = taskCall.Vars
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("cannot unmarshal %s into dependency", node.ShortTag())
|
|
||||||
}
|
|
||||||
|
@ -1,23 +1,35 @@
|
|||||||
package taskfile
|
package taskfile
|
||||||
|
|
||||||
import "golang.org/x/exp/constraints"
|
type DeepCopier[T any] interface {
|
||||||
|
DeepCopy() T
|
||||||
|
}
|
||||||
|
|
||||||
func deepCopySlice[T any](orig []T) []T {
|
func deepCopySlice[T any](orig []T) []T {
|
||||||
if orig == nil {
|
if orig == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c := make([]T, len(orig))
|
c := make([]T, len(orig))
|
||||||
copy(c, orig)
|
for i, v := range orig {
|
||||||
|
if copyable, ok := any(v).(DeepCopier[T]); ok {
|
||||||
|
c[i] = copyable.DeepCopy()
|
||||||
|
} else {
|
||||||
|
c[i] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func deepCopyMap[K constraints.Ordered, V any](orig map[K]V) map[K]V {
|
func deepCopyMap[K comparable, V any](orig map[K]V) map[K]V {
|
||||||
if orig == nil {
|
if orig == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c := make(map[K]V, len(orig))
|
c := make(map[K]V, len(orig))
|
||||||
for k, v := range orig {
|
for k, v := range orig {
|
||||||
c[k] = v
|
if copyable, ok := any(v).(DeepCopier[V]); ok {
|
||||||
|
c[k] = copyable.DeepCopy()
|
||||||
|
} else {
|
||||||
|
c[k] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
50
taskfile/dep.go
Normal file
50
taskfile/dep.go
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
package taskfile
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Dep is a task dependency
|
||||||
|
type Dep struct {
|
||||||
|
Task string
|
||||||
|
Vars *Vars
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Dep) DeepCopy() *Dep {
|
||||||
|
if d == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &Dep{
|
||||||
|
Task: d.Task,
|
||||||
|
Vars: d.Vars.DeepCopy(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Dep) UnmarshalYAML(node *yaml.Node) error {
|
||||||
|
switch node.Kind {
|
||||||
|
|
||||||
|
case yaml.ScalarNode:
|
||||||
|
var task string
|
||||||
|
if err := node.Decode(&task); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.Task = task
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case yaml.MappingNode:
|
||||||
|
var taskCall struct {
|
||||||
|
Task string
|
||||||
|
Vars *Vars
|
||||||
|
}
|
||||||
|
if err := node.Decode(&taskCall); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.Task = taskCall.Task
|
||||||
|
d.Vars = taskCall.Vars
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("cannot unmarshal %s into dependency", node.ShortTag())
|
||||||
|
}
|
@ -15,6 +15,16 @@ type Platform struct {
|
|||||||
Arch string
|
Arch string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Platform) DeepCopy() *Platform {
|
||||||
|
if p == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &Platform{
|
||||||
|
OS: p.OS,
|
||||||
|
Arch: p.Arch,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type ErrInvalidPlatform struct {
|
type ErrInvalidPlatform struct {
|
||||||
Platform string
|
Platform string
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,16 @@ type Precondition struct {
|
|||||||
Msg string
|
Msg string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Precondition) DeepCopy() *Precondition {
|
||||||
|
if p == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &Precondition{
|
||||||
|
Sh: p.Sh,
|
||||||
|
Msg: p.Msg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
// UnmarshalYAML implements yaml.Unmarshaler interface.
|
||||||
func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
|
func (p *Precondition) UnmarshalYAML(node *yaml.Node) error {
|
||||||
switch node.Kind {
|
switch node.Kind {
|
||||||
|
@ -131,6 +131,9 @@ func (t *Task) UnmarshalYAML(node *yaml.Node) error {
|
|||||||
// DeepCopy creates a new instance of Task and copies
|
// DeepCopy creates a new instance of Task and copies
|
||||||
// data by value from the source struct.
|
// data by value from the source struct.
|
||||||
func (t *Task) DeepCopy() *Task {
|
func (t *Task) DeepCopy() *Task {
|
||||||
|
if t == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
c := &Task{
|
c := &Task{
|
||||||
Task: t.Task,
|
Task: t.Task,
|
||||||
Cmds: deepCopySlice(t.Cmds),
|
Cmds: deepCopySlice(t.Cmds),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user