-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from hyphennn/hyphen/dev
feat: support conv
- Loading branch information
Showing
8 changed files
with
158 additions
and
1 deletion.
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
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,32 @@ | ||
// Package gconv | ||
// Author: hyphen | ||
// Copyright 2023 hyphen. All rights reserved. | ||
// Create-time: 2023/12/20 | ||
package gconv | ||
|
||
import ( | ||
"github.com/hyphennn/glamda/internal" | ||
"github.com/hyphennn/glamda/internal/constraints" | ||
) | ||
|
||
func ToPtr[T constraints.UnPtrAble](t T) *T { | ||
return &t | ||
} | ||
|
||
func FromPtr[T constraints.UnPtrAble](t *T) T { | ||
if t == nil { | ||
return internal.Zero[T]() | ||
} | ||
return *t | ||
} | ||
|
||
func StringPtr(s string) *string { | ||
return &s | ||
} | ||
|
||
func Ptr2String(s *string) string { | ||
if s == nil { | ||
return "" | ||
} | ||
return *s | ||
} |
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,22 @@ | ||
// Package gconv | ||
// Author: hyphen | ||
// Copyright 2023 hyphen. All rights reserved. | ||
// Create-time: 2023/12/20 | ||
package gconv_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyphennn/glamda/gconv" | ||
"github.com/hyphennn/glamda/internal/assert" | ||
) | ||
|
||
func TestToPtr(t *testing.T) { | ||
s := gconv.ToPtr("123") | ||
assert.Equal(t, "123", *s) | ||
} | ||
|
||
func TestFromPtr(t *testing.T) { | ||
s := "123" | ||
assert.Equal(t, "123", gconv.FromPtr(&s)) | ||
} |
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,13 @@ | ||
// Package gstream | ||
// Author: hyphen | ||
// Copyright 2023 hyphen. All rights reserved. | ||
// Create-time: 2023/12/11 | ||
package gstream | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test0(t *testing.T) { | ||
|
||
} |
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,33 @@ | ||
// Package gstream | ||
// Author: hyphen | ||
// Copyright 2023 hyphen. All rights reserved. | ||
// Create-time: 2023/12/11 | ||
package gstream | ||
|
||
type MapStream[K comparable, V any] struct { | ||
m map[K]V | ||
s *SliceStream[K] | ||
} | ||
|
||
func AsMapStream[K comparable, V any, M ~map[K]V](m M) *MapStream[K, V] { | ||
return &MapStream[K, V]{m, nil} | ||
} | ||
|
||
func ToSliceStream[K comparable, T, V any](m *MapStream[K, V], fc func(K, V) T) *SliceStream[T] { | ||
s := make([]T, 0, len(m.m)) | ||
m.ForEach(func(k K, v V) { | ||
s = append(s, fc(k, v)) | ||
}) | ||
return AsSliceStream(s) | ||
} | ||
|
||
func (m *MapStream[K, V]) ForEach(fc func(K, V)) *MapStream[K, V] { | ||
for k, v := range m.m { | ||
fc(k, v) | ||
} | ||
return m | ||
} | ||
|
||
func (m *MapStream[K, V]) Collect() map[K]V { | ||
return m.m | ||
} |
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,36 @@ | ||
// Package gstream | ||
// Author: hyphen | ||
// Copyright 2023 hyphen. All rights reserved. | ||
// Create-time: 2023/12/11 | ||
package gstream | ||
|
||
type SliceStream[T any] struct { | ||
s []T | ||
} | ||
|
||
func AsSliceStream[T any](s []T) *SliceStream[T] { | ||
return &SliceStream[T]{s} | ||
} | ||
|
||
func ToMapStream[K comparable, T, V any](s *SliceStream[T], fc func(T) (K, V)) *MapStream[K, V] { | ||
m := make(map[K]V, len(s.s)) | ||
s.ForEach(func(t T) { | ||
k, v := fc(t) | ||
m[k] = v | ||
}) | ||
return AsMapStream(m) | ||
} | ||
|
||
func (s *SliceStream[T]) ForEach(fc func(T)) { | ||
for _, v := range s.s { | ||
fc(v) | ||
} | ||
} | ||
|
||
func (s *SliceStream[T]) Count() int { | ||
return len(s.s) | ||
} | ||
|
||
func (s *SliceStream[T]) Collect() []T { | ||
return s.s | ||
} |
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
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