Skip to content

Commit

Permalink
修改代码
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdogs committed Jun 5, 2024
1 parent caca1f8 commit f17b0c3
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 27 deletions.
5 changes: 3 additions & 2 deletions ec/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"git.golaxy.org/core/internal/gctx"
"git.golaxy.org/core/utils/container"
"git.golaxy.org/core/utils/iface"
"git.golaxy.org/core/utils/meta"
"git.golaxy.org/core/utils/option"
"git.golaxy.org/core/utils/reinterpret"
"git.golaxy.org/core/utils/uid"
Expand Down Expand Up @@ -50,7 +51,7 @@ type Entity interface {
// GetReflected 获取反射值
GetReflected() reflect.Value
// GetMeta 获取Meta信息
GetMeta() Meta
GetMeta() meta.Meta
// DestroySelf 销毁自身
DestroySelf()
}
Expand Down Expand Up @@ -117,7 +118,7 @@ func (entity *EntityBehavior) GetReflected() reflect.Value {
}

// GetMeta 获取Meta信息
func (entity *EntityBehavior) GetMeta() Meta {
func (entity *EntityBehavior) GetMeta() meta.Meta {
return entity.opts.Meta
}

Expand Down
5 changes: 3 additions & 2 deletions ec/entity_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec

import (
"git.golaxy.org/core/utils/iface"
"git.golaxy.org/core/utils/meta"
"git.golaxy.org/core/utils/option"
"git.golaxy.org/core/utils/uid"
)
Expand All @@ -13,7 +14,7 @@ type EntityOptions struct {
Scope Scope // 可访问作用域
PersistId uid.Id // 实体持久化Id
AwakeOnFirstAccess bool // 开启组件被首次访问时,检测并调用Awake()
Meta Meta // Meta信息
Meta meta.Meta // Meta信息
}

var With _Option
Expand Down Expand Up @@ -68,7 +69,7 @@ func (_Option) AwakeOnFirstAccess(b bool) option.Setting[EntityOptions] {
}

// Meta Meta信息
func (_Option) Meta(m Meta) option.Setting[EntityOptions] {
func (_Option) Meta(m meta.Meta) option.Setting[EntityOptions] {
return func(o *EntityOptions) {
o.Meta = m
}
Expand Down
22 changes: 0 additions & 22 deletions ec/meta.go

This file was deleted.

3 changes: 2 additions & 1 deletion entitycreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"git.golaxy.org/core/runtime"
"git.golaxy.org/core/service"
"git.golaxy.org/core/utils/iface"
"git.golaxy.org/core/utils/meta"
"git.golaxy.org/core/utils/option"
"git.golaxy.org/core/utils/uid"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ func (c EntityCreator) AwakeOnFirstAccess(b bool) EntityCreator {
}

// Meta 设置Meta信息
func (c EntityCreator) Meta(m ec.Meta) EntityCreator {
func (c EntityCreator) Meta(m meta.Meta) EntityCreator {
c.settings = append(c.settings, ec.With.Meta(m))
return c
}
Expand Down
126 changes: 126 additions & 0 deletions utils/generic/slicemap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package generic

import (
"git.golaxy.org/core/utils/types"
"slices"
)

func MakeSliceMap[K comparable, V any](kvs ...KV[K, V]) SliceMap[K, V] {
kvs = slices.Clone(kvs)
for i := len(kvs) - 1; i >= 0; i-- {
it := kvs[i]

if !slices.ContainsFunc(kvs[:i], func(kv KV[K, V]) bool {
return kv.K == it.K
}) {
continue
}

kvs = append(kvs[:i], kvs[i+1:]...)
}
return kvs
}

func NewSliceMap[K comparable, V any](kvs ...KV[K, V]) *SliceMap[K, V] {
kvs = slices.Clone(kvs)
for i := len(kvs) - 1; i >= 0; i-- {
it := kvs[i]

if !slices.ContainsFunc(kvs[:i], func(kv KV[K, V]) bool {
return kv.K == it.K
}) {
continue
}

kvs = append(kvs[:i], kvs[i+1:]...)
}
return (*SliceMap[K, V])(&kvs)
}

func MakeSliceMapFromGoMap[K comparable, V any](m map[K]V) SliceMap[K, V] {
sm := make(SliceMap[K, V], 0, len(m))
for k, v := range m {
sm = append(sm, KV[K, V]{K: k, V: v})
}
return sm
}

func NewSliceMapFromGoMap[K comparable, V any](m map[K]V) *SliceMap[K, V] {
sm := make(SliceMap[K, V], 0, len(m))
for k, v := range m {
sm = append(sm, KV[K, V]{K: k, V: v})
}
return &sm
}

type KV[K comparable, V any] struct {
K K
V V
}

type SliceMap[K comparable, V any] []KV[K, V]

func (m *SliceMap[K, V]) Add(k K, v V) {
idx := slices.IndexFunc(*m, func(kv KV[K, V]) bool {
return kv.K == k
})
if idx >= 0 {
(*m)[idx] = KV[K, V]{K: k, V: v}
return
}
*m = append(*m, KV[K, V]{K: k, V: v})
}

func (m *SliceMap[K, V]) TryAdd(k K, v V) bool {
idx := slices.IndexFunc(*m, func(kv KV[K, V]) bool {
return kv.K == k
})
if idx >= 0 {
return false
}
*m = append(*m, KV[K, V]{K: k, V: v})
return true
}

func (m *SliceMap[K, V]) Delete(k K) bool {
var ok bool
*m = slices.DeleteFunc(*m, func(kv KV[K, V]) bool {
ok = kv.K == k
return ok
})
return ok
}

func (m SliceMap[K, V]) Get(k K) (V, bool) {
idx := slices.IndexFunc(m, func(kv KV[K, V]) bool {
return kv.K == k
})
if idx >= 0 {
return m[idx].V, true
}
return types.ZeroT[V](), false
}

func (m SliceMap[K, V]) Value(k K) V {
idx := slices.IndexFunc(m, func(kv KV[K, V]) bool {
return kv.K == k
})
if idx >= 0 {
return m[idx].V
}
return types.ZeroT[V]()
}

func (m SliceMap[K, V]) Exist(k K) bool {
return slices.ContainsFunc(m, func(kv KV[K, V]) bool {
return kv.K == k
})
}

func (m SliceMap[K, V]) ToGoMap() map[K]V {
rv := make(map[K]V, len(m))
for _, kv := range m {
rv[kv.K] = kv.V
}
return rv
}
46 changes: 46 additions & 0 deletions utils/meta/meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package meta

import "git.golaxy.org/core/utils/generic"

type Meta = generic.SliceMap[string, any]

type _MetaCtor struct {
meta Meta
}

func (c _MetaCtor) Add(k string, v any) _MetaCtor {
c.meta.Add(k, v)
return c
}

func (c _MetaCtor) Delete(k string) _MetaCtor {
c.meta.Delete(k)
return c
}

func (c _MetaCtor) Combine(m map[string]any) _MetaCtor {
for k, v := range m {
c.meta.TryAdd(k, v)
}
return c
}

func (c _MetaCtor) Override(m map[string]any) _MetaCtor {
for k, v := range m {
c.meta.Add(k, v)
}
return c
}

func (c _MetaCtor) Clean() _MetaCtor {
c.meta = c.meta[:0]
return c
}

func (c _MetaCtor) Get() Meta {
return c.meta
}

func Make() _MetaCtor {
return _MetaCtor{}
}

0 comments on commit f17b0c3

Please sign in to comment.