From 841e8694e97b22ee56f26ea3cb8ca9b055514bce Mon Sep 17 00:00:00 2001 From: siyul-park Date: Wed, 29 Nov 2023 01:36:14 -0500 Subject: [PATCH] refactor: primitive.Object -> Value --- pkg/database/collection.go | 6 ++-- pkg/database/filter.go | 18 +++++----- pkg/database/memdb/collection.go | 14 ++++---- pkg/database/memdb/compare.go | 2 +- pkg/database/memdb/filter.go | 20 +++++------ pkg/database/memdb/index.go | 16 ++++----- pkg/database/memdb/sort.go | 4 +-- pkg/database/mongodb/collection.go | 14 ++++---- pkg/database/mongodb/encoding.go | 30 ++++++++-------- pkg/database/mongodb/stream.go | 2 +- pkg/packet/packet.go | 8 ++--- pkg/plugin/networkx/http.go | 18 +++++----- pkg/plugin/networkx/mime.go | 10 +++--- pkg/plugin/networkx/mime_test.go | 4 +-- pkg/plugin/networkx/router.go | 2 +- pkg/plugin/systemx/reflect.go | 2 +- pkg/primitive/binary.go | 12 +++---- pkg/primitive/bool.go | 12 +++---- pkg/primitive/encoding.go | 12 +++---- pkg/primitive/encoding_test.go | 6 ++-- pkg/primitive/float.go | 16 ++++----- pkg/primitive/int.go | 20 +++++------ pkg/primitive/map.go | 38 ++++++++++---------- pkg/primitive/object.go | 53 --------------------------- pkg/primitive/pick.go | 2 +- pkg/primitive/pointer.go | 8 ++--- pkg/primitive/shortcut.go | 12 +++---- pkg/primitive/slice.go | 28 +++++++-------- pkg/primitive/string.go | 12 +++---- pkg/primitive/uint.go | 20 +++++------ pkg/primitive/value.go | 57 ++++++++++++++++++++++++++++++ 31 files changed, 241 insertions(+), 237 deletions(-) delete mode 100644 pkg/primitive/object.go create mode 100644 pkg/primitive/value.go diff --git a/pkg/database/collection.go b/pkg/database/collection.go index ce0bd5bc..604b8375 100644 --- a/pkg/database/collection.go +++ b/pkg/database/collection.go @@ -15,8 +15,8 @@ type ( Watch(ctx context.Context, filter *Filter) (Stream, error) - InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error) - InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error) + InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error) + InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error) UpdateOne(ctx context.Context, filter *Filter, patch *primitive.Map, options ...*UpdateOptions) (bool, error) UpdateMany(ctx context.Context, filter *Filter, patch *primitive.Map, options ...*UpdateOptions) (int, error) @@ -48,7 +48,7 @@ type ( Event struct { OP eventOP - DocumentID primitive.Object + DocumentID primitive.Value } eventOP int diff --git a/pkg/database/filter.go b/pkg/database/filter.go index 3b212b91..94f73f09 100644 --- a/pkg/database/filter.go +++ b/pkg/database/filter.go @@ -13,7 +13,7 @@ type ( Filter struct { OP OP Key string - Value primitive.Object + Value primitive.Value Children []*Filter } @@ -45,7 +45,7 @@ func Where(key string) *filterHelper { } } -func (fh *filterHelper) EQ(value primitive.Object) *Filter { +func (fh *filterHelper) EQ(value primitive.Value) *Filter { return &Filter{ OP: EQ, Key: fh.key, @@ -53,7 +53,7 @@ func (fh *filterHelper) EQ(value primitive.Object) *Filter { } } -func (fh *filterHelper) NE(value primitive.Object) *Filter { +func (fh *filterHelper) NE(value primitive.Value) *Filter { return &Filter{ OP: NE, Key: fh.key, @@ -61,7 +61,7 @@ func (fh *filterHelper) NE(value primitive.Object) *Filter { } } -func (fh *filterHelper) LT(value primitive.Object) *Filter { +func (fh *filterHelper) LT(value primitive.Value) *Filter { return &Filter{ Key: fh.key, OP: LT, @@ -69,7 +69,7 @@ func (fh *filterHelper) LT(value primitive.Object) *Filter { } } -func (fh *filterHelper) LTE(value primitive.Object) *Filter { +func (fh *filterHelper) LTE(value primitive.Value) *Filter { return &Filter{ OP: LTE, Key: fh.key, @@ -77,7 +77,7 @@ func (fh *filterHelper) LTE(value primitive.Object) *Filter { } } -func (fh *filterHelper) GT(value primitive.Object) *Filter { +func (fh *filterHelper) GT(value primitive.Value) *Filter { return &Filter{ OP: GT, Key: fh.key, @@ -85,7 +85,7 @@ func (fh *filterHelper) GT(value primitive.Object) *Filter { } } -func (fh *filterHelper) GTE(value primitive.Object) *Filter { +func (fh *filterHelper) GTE(value primitive.Value) *Filter { return &Filter{ OP: GTE, Key: fh.key, @@ -93,7 +93,7 @@ func (fh *filterHelper) GTE(value primitive.Object) *Filter { } } -func (fh *filterHelper) IN(slice ...primitive.Object) *Filter { +func (fh *filterHelper) IN(slice ...primitive.Value) *Filter { return &Filter{ OP: IN, Key: fh.key, @@ -101,7 +101,7 @@ func (fh *filterHelper) IN(slice ...primitive.Object) *Filter { } } -func (fh *filterHelper) NotIN(slice ...primitive.Object) *Filter { +func (fh *filterHelper) NotIN(slice ...primitive.Value) *Filter { return &Filter{ OP: NIN, Key: fh.key, diff --git a/pkg/database/memdb/collection.go b/pkg/database/memdb/collection.go index f1b36502..e0d533bf 100644 --- a/pkg/database/memdb/collection.go +++ b/pkg/database/memdb/collection.go @@ -85,7 +85,7 @@ func (coll *Collection) Watch(ctx context.Context, filter *database.Filter) (dat return stream, nil } -func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error) { +func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error) { if id, err := coll.insertOne(ctx, doc); err != nil { return nil, err } else { @@ -100,7 +100,7 @@ func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (prim } } -func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error) { +func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error) { if ids, err := coll.insertMany(ctx, docs); err != nil { return nil, err } else { @@ -132,7 +132,7 @@ func (coll *Collection) UpdateOne(ctx context.Context, filter *database.Filter, return false, nil } - var id primitive.Object + var id primitive.Value if old != nil { id = old.GetOr(keyID, nil) } @@ -345,7 +345,7 @@ func (coll *Collection) Drop(ctx context.Context) error { return nil } -func (coll *Collection) insertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error) { +func (coll *Collection) insertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error) { if ids, err := coll.insertMany(ctx, []*primitive.Map{doc}); err != nil { return nil, err } else { @@ -353,11 +353,11 @@ func (coll *Collection) insertOne(ctx context.Context, doc *primitive.Map) (prim } } -func (coll *Collection) insertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error) { +func (coll *Collection) insertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error) { coll.dataLock.Lock() defer coll.dataLock.Unlock() - ids := make([]primitive.Object, len(docs)) + ids := make([]primitive.Value, len(docs)) for i, doc := range docs { if id, ok := doc.Get(keyID); !ok { return nil, errors.Wrap(errors.WithStack(ErrPKNotFound), database.ErrCodeWrite) @@ -481,7 +481,7 @@ func (coll *Collection) deleteMany(ctx context.Context, docs []*primitive.Map) ( coll.dataLock.Lock() defer coll.dataLock.Unlock() - ids := make([]primitive.Object, 0, len(docs)) + ids := make([]primitive.Value, 0, len(docs)) deletes := make([]*primitive.Map, 0, len(docs)) for _, doc := range docs { if doc == nil { diff --git a/pkg/database/memdb/compare.go b/pkg/database/memdb/compare.go index 7d68e0f2..573f829f 100644 --- a/pkg/database/memdb/compare.go +++ b/pkg/database/memdb/compare.go @@ -7,6 +7,6 @@ import ( var ( comparator = utils.Comparator(func(a, b any) int { - return primitive.Compare(a.(primitive.Object), b.(primitive.Object)) + return primitive.Compare(a.(primitive.Value), b.(primitive.Value)) }) ) diff --git a/pkg/database/memdb/filter.go b/pkg/database/memdb/filter.go index ea3f70c4..55af3727 100644 --- a/pkg/database/memdb/filter.go +++ b/pkg/database/memdb/filter.go @@ -22,7 +22,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { switch filter.OP { case database.EQ: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) == 0 @@ -30,7 +30,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NE: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) != 0 @@ -38,7 +38,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.LT: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) < 0 @@ -46,7 +46,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.LTE: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) <= 0 @@ -54,7 +54,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.GT: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) > 0 @@ -62,7 +62,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.GTE: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return primitive.Compare(o, filter.Value) >= 0 @@ -70,7 +70,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.IN: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else if o == nil { return false @@ -87,7 +87,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NIN: return func(m *primitive.Map) bool { - if o, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if o, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return true } else if o == nil { return true @@ -104,7 +104,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NULL: return func(m *primitive.Map) bool { - if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if v, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return v == nil @@ -112,7 +112,7 @@ func ParseFilter(filter *database.Filter) func(*primitive.Map) bool { } case database.NNULL: return func(m *primitive.Map) bool { - if v, ok := primitive.Pick[primitive.Object](m, parsePath(filter.Key)...); !ok { + if v, ok := primitive.Pick[primitive.Value](m, parsePath(filter.Key)...); !ok { return false } else { return v != nil diff --git a/pkg/database/memdb/index.go b/pkg/database/memdb/index.go index 8d0232f3..4b27ca68 100644 --- a/pkg/database/memdb/index.go +++ b/pkg/database/memdb/index.go @@ -134,7 +134,7 @@ func (iv *IndexView) deleteAll(_ context.Context) error { return nil } -func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]primitive.Object, error) { +func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]primitive.Value, error) { iv.lock.RLock() defer iv.lock.RUnlock() @@ -158,7 +158,7 @@ func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]p var i int var k string for i, k = range model.Keys { - if obj, ok := primitive.Pick[primitive.Object](example, k); ok { + if obj, ok := primitive.Pick[primitive.Value](example, k); ok { visits[k] = true if sub, ok := curr.Get(obj); ok { if i < len(model.Keys)-1 { @@ -220,9 +220,9 @@ func (iv *IndexView) findMany(_ context.Context, examples []*primitive.Map) ([]p } } - var uniqueIds []primitive.Object + var uniqueIds []primitive.Value for _, v := range ids.Values() { - uniqueIds = append(uniqueIds, v.(primitive.Object)) + uniqueIds = append(uniqueIds, v.(primitive.Value)) } return uniqueIds, nil } @@ -242,7 +242,7 @@ func (iv *IndexView) insertOne(ctx context.Context, doc *primitive.Map) error { } for i, k := range model.Keys { - obj, _ := primitive.Pick[primitive.Object](doc, k) + obj, _ := primitive.Pick[primitive.Value](doc, k) if i < len(model.Keys)-1 { sub, ok := curr.Get(obj) @@ -293,11 +293,11 @@ func (iv *IndexView) deleteOne(_ context.Context, doc *primitive.Map) error { var nodes []containers.Container nodes = append(nodes, curr) - var keys []primitive.Object + var keys []primitive.Value keys = append(keys, nil) for i, k := range model.Keys { - obj, _ := primitive.Pick[primitive.Object](doc, k) + obj, _ := primitive.Pick[primitive.Value](doc, k) if i < len(model.Keys)-1 { if sub, ok := curr.Get(obj); ok { @@ -309,7 +309,7 @@ func (iv *IndexView) deleteOne(_ context.Context, doc *primitive.Map) error { return nil } } else if model.Unique { - if r, ok := curr.Get(obj); ok && primitive.Compare(id, r.(primitive.Object)) == 0 { + if r, ok := curr.Get(obj); ok && primitive.Compare(id, r.(primitive.Value)) == 0 { curr.Remove(obj) } } else { diff --git a/pkg/database/memdb/sort.go b/pkg/database/memdb/sort.go index ccabc69f..4329b14f 100644 --- a/pkg/database/memdb/sort.go +++ b/pkg/database/memdb/sort.go @@ -8,8 +8,8 @@ import ( func ParseSorts(sorts []database.Sort) func(i, j *primitive.Map) bool { return func(i, j *primitive.Map) bool { for _, s := range sorts { - x, _ := primitive.Pick[primitive.Object](i, parsePath(s.Key)...) - y, _ := primitive.Pick[primitive.Object](j, parsePath(s.Key)...) + x, _ := primitive.Pick[primitive.Value](i, parsePath(s.Key)...) + y, _ := primitive.Pick[primitive.Value](j, parsePath(s.Key)...) if x == y { continue diff --git a/pkg/database/mongodb/collection.go b/pkg/database/mongodb/collection.go index 63285f11..58be382a 100644 --- a/pkg/database/mongodb/collection.go +++ b/pkg/database/mongodb/collection.go @@ -59,7 +59,7 @@ func (coll *Collection) Watch(ctx context.Context, filter *database.Filter) (dat return UpgradeStream(stream), nil } -func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Object, error) { +func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (primitive.Value, error) { raw, err := MarshalDocument(doc) if err != nil { return nil, err @@ -70,14 +70,14 @@ func (coll *Collection) InsertOne(ctx context.Context, doc *primitive.Map) (prim return nil, errors.Wrap(database.ErrWrite, err.Error()) } - var id primitive.Object + var id primitive.Value if err := UnmarshalDocument(res.InsertedID, &id); err != nil { return nil, err } return id, nil } -func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Object, error) { +func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ([]primitive.Value, error) { var raws bson.A for _, doc := range docs { if raw, err := MarshalDocument(doc); err == nil { @@ -92,9 +92,9 @@ func (coll *Collection) InsertMany(ctx context.Context, docs []*primitive.Map) ( return nil, errors.Wrap(database.ErrWrite, err.Error()) } - var ids []primitive.Object + var ids []primitive.Value for _, insertedID := range res.InsertedIDs { - var id primitive.Object + var id primitive.Value if err := UnmarshalDocument(insertedID, &id); err != nil { return nil, err } @@ -182,7 +182,7 @@ func (coll *Collection) FindOne(ctx context.Context, filter *database.Filter, op return nil, errors.Wrap(database.ErrRead, res.Err().Error()) } - var doc primitive.Object + var doc primitive.Value var r any if err := res.Decode(&r); err != nil { return nil, err @@ -206,7 +206,7 @@ func (coll *Collection) FindMany(ctx context.Context, filter *database.Filter, o var docs []*primitive.Map for cursor.Next(ctx) { - var doc primitive.Object + var doc primitive.Value var r any if err := cursor.Decode(&r); err != nil { return nil, err diff --git a/pkg/database/mongodb/encoding.go b/pkg/database/mongodb/encoding.go index 815e8136..258b3a62 100644 --- a/pkg/database/mongodb/encoding.go +++ b/pkg/database/mongodb/encoding.go @@ -33,15 +33,15 @@ func UnmarshalFilter(data any, v **database.Filter) error { return filterDecoder.Decode(data, v) } -func MarshalDocument(v primitive.Object) (any, error) { +func MarshalDocument(v primitive.Value) (any, error) { return documentEncoder.Encode(v) } -func UnmarshalDocument(data any, v *primitive.Object) error { +func UnmarshalDocument(data any, v *primitive.Value) error { return documentDecoder.Decode(data, v) } -func NewFilterEncoder(encoder encoding.Encoder[primitive.Object, any]) encoding.Encoder[*database.Filter, any] { +func NewFilterEncoder(encoder encoding.Encoder[primitive.Value, any]) encoding.Encoder[*database.Filter, any] { return encoding.EncoderFunc[*database.Filter, any](func(source *database.Filter) (any, error) { if source == nil { return bson.D{}, nil @@ -103,7 +103,7 @@ func NewFilterEncoder(encoder encoding.Encoder[primitive.Object, any]) encoding. }) } -func NewFilterDecoder(decoder encoding.Decoder[any, *primitive.Object]) encoding.Decoder[any, **database.Filter] { +func NewFilterDecoder(decoder encoding.Decoder[any, *primitive.Value]) encoding.Decoder[any, **database.Filter] { return encoding.DecoderFunc[any, **database.Filter](func(source any, target **database.Filter) error { s, ok := bsonMA(source) if !ok { @@ -197,7 +197,7 @@ func NewFilterDecoder(decoder encoding.Decoder[any, *primitive.Object]) encoding return errors.WithStack(encoding.ErrUnsupportedValue) } - var value primitive.Object + var value primitive.Value if err := decoder.Decode(v, &value); err != nil { return err } @@ -225,8 +225,8 @@ func NewFilterDecoder(decoder encoding.Decoder[any, *primitive.Object]) encoding }) } -func NewDocumentEncoder() encoding.Encoder[primitive.Object, any] { - return encoding.EncoderFunc[primitive.Object, any](func(source primitive.Object) (any, error) { +func NewDocumentEncoder() encoding.Encoder[primitive.Value, any] { + return encoding.EncoderFunc[primitive.Value, any](func(source primitive.Value) (any, error) { if source == nil { return bsonprimitive.Null{}, nil } @@ -266,8 +266,8 @@ func NewDocumentEncoder() encoding.Encoder[primitive.Object, any] { }) } -func NewDocumentDecoder() encoding.Decoder[any, *primitive.Object] { - return encoding.DecoderFunc[any, *primitive.Object](func(source any, target *primitive.Object) error { +func NewDocumentDecoder() encoding.Decoder[any, *primitive.Value] { + return encoding.DecoderFunc[any, *primitive.Value](func(source any, target *primitive.Value) error { self := NewDocumentDecoder() if source == nil { @@ -283,9 +283,9 @@ func NewDocumentDecoder() encoding.Decoder[any, *primitive.Object] { *target = primitive.NewBinary(s.Data) return nil } else if s, ok := source.(bsonprimitive.A); ok { - values := make([]primitive.Object, len(s)) + values := make([]primitive.Value, len(s)) for i, e := range s { - var value primitive.Object + var value primitive.Value if err := self.Decode(e, &value); err != nil { return err } @@ -294,9 +294,9 @@ func NewDocumentDecoder() encoding.Decoder[any, *primitive.Object] { *target = primitive.NewSlice(values...) return nil } else if s, ok := source.(bsonprimitive.D); ok { - pairs := make([]primitive.Object, len(s)*2) + pairs := make([]primitive.Value, len(s)*2) for i, e := range s { - var value primitive.Object + var value primitive.Value if err := self.Decode(e.Value, &value); err != nil { return err } @@ -306,10 +306,10 @@ func NewDocumentDecoder() encoding.Decoder[any, *primitive.Object] { *target = primitive.NewMap(pairs...) return nil } else if s, ok := source.(bsonprimitive.M); ok { - pairs := make([]primitive.Object, len(s)*2) + pairs := make([]primitive.Value, len(s)*2) i := 0 for k, v := range s { - var value primitive.Object + var value primitive.Value if err := self.Decode(v, &value); err != nil { return err } diff --git a/pkg/database/mongodb/stream.go b/pkg/database/mongodb/stream.go index 9188eb84..7ad7ab94 100644 --- a/pkg/database/mongodb/stream.go +++ b/pkg/database/mongodb/stream.go @@ -43,7 +43,7 @@ func UpgradeStream(stream *mongo.ChangeStream) *Stream { return } - var id primitive.Object + var id primitive.Value if documentKey, ok := data["documentKey"]; ok { if documentKey, ok := documentKey.(bson.M); ok { if err := UnmarshalDocument(documentKey["_id"], &id); err != nil { diff --git a/pkg/packet/packet.go b/pkg/packet/packet.go index 500d0f27..2eaebdea 100644 --- a/pkg/packet/packet.go +++ b/pkg/packet/packet.go @@ -9,13 +9,13 @@ type ( // Packet is a formalized block of data. Packet struct { id ulid.ULID - payload primitive.Object + payload primitive.Value } ) // NewError return a new Packet for error. func NewError(err error, cause *Packet) *Packet { - var pairs []primitive.Object + var pairs []primitive.Value pairs = append(pairs, primitive.NewString("error"), primitive.NewString(err.Error())) if cause != nil { pairs = append(pairs, primitive.NewString("cause"), cause.Payload()) @@ -25,7 +25,7 @@ func NewError(err error, cause *Packet) *Packet { } // New returns a new Packet. -func New(payload primitive.Object) *Packet { +func New(payload primitive.Value) *Packet { return &Packet{ id: ulid.Make(), payload: payload, @@ -38,6 +38,6 @@ func (pck *Packet) ID() ulid.ULID { } // Payload returns the payload of the Packet. -func (pck *Packet) Payload() primitive.Object { +func (pck *Packet) Payload() primitive.Value { return pck.payload } diff --git a/pkg/plugin/networkx/http.go b/pkg/plugin/networkx/http.go index 8e00ddeb..9d675772 100644 --- a/pkg/plugin/networkx/http.go +++ b/pkg/plugin/networkx/http.go @@ -52,14 +52,14 @@ type ( // HTTPPayload represents the payload for HTTP requests and responses. HTTPPayload struct { - Proto string `map:"proto,omitempty"` - Path string `map:"path,omitempty"` - Method string `map:"method,omitempty"` - Header http.Header `map:"header,omitempty"` - Query url.Values `map:"query,omitempty"` - Cookies []*http.Cookie `map:"cookies,omitempty"` - Body primitive.Object `map:"body,omitempty"` - Status int `map:"status"` + Proto string `map:"proto,omitempty"` + Path string `map:"path,omitempty"` + Method string `map:"method,omitempty"` + Header http.Header `map:"header,omitempty"` + Query url.Values `map:"query,omitempty"` + Cookies []*http.Cookie `map:"cookies,omitempty"` + Body primitive.Value `map:"body,omitempty"` + Status int `map:"status"` } tcpKeepAliveListener struct { @@ -584,7 +584,7 @@ func (n *HTTPNode) configureServer() error { return nil } -func NewHTTPPayload(status int, body ...primitive.Object) HTTPPayload { +func NewHTTPPayload(status int, body ...primitive.Value) HTTPPayload { he := HTTPPayload{Status: status, Body: primitive.NewString(http.StatusText(status))} if len(body) > 0 { he.Body = body[0] diff --git a/pkg/plugin/networkx/mime.go b/pkg/plugin/networkx/mime.go index 45060808..f73287c9 100644 --- a/pkg/plugin/networkx/mime.go +++ b/pkg/plugin/networkx/mime.go @@ -41,7 +41,7 @@ const ( charsetUTF8 = "charset=utf-8" ) -func MarshalMIME(value primitive.Object, typ *string) ([]byte, error) { +func MarshalMIME(value primitive.Value, typ *string) ([]byte, error) { if typ == nil { content := "" typ = &content @@ -114,7 +114,7 @@ func MarshalMIME(value primitive.Object, typ *string) ([]byte, error) { return nil, err } - writeField := func(obj *primitive.Map, key primitive.Object) error { + writeField := func(obj *primitive.Map, key primitive.Value) error { if key, ok := key.(primitive.String); ok { elements := obj.GetOr(key, nil) if e, ok := elements.(primitive.String); ok { @@ -133,7 +133,7 @@ func MarshalMIME(value primitive.Object, typ *string) ([]byte, error) { } return nil } - writeFields := func(value primitive.Object) error { + writeFields := func(value primitive.Value) error { if value, ok := value.(*primitive.Map); ok { for _, key := range value.Keys() { if err := writeField(value, key); err != nil { @@ -144,7 +144,7 @@ func MarshalMIME(value primitive.Object, typ *string) ([]byte, error) { return nil } - writeFiles := func(value primitive.Object) error { + writeFiles := func(value primitive.Value) error { if value, ok := value.(*primitive.Map); ok { for _, key := range value.Keys() { if key, ok := key.(primitive.String); ok { @@ -202,7 +202,7 @@ func MarshalMIME(value primitive.Object, typ *string) ([]byte, error) { return nil, errors.WithStack(encoding.ErrUnsupportedValue) } -func UnmarshalMIME(data []byte, typ *string) (primitive.Object, error) { +func UnmarshalMIME(data []byte, typ *string) (primitive.Value, error) { if len(data) == 0 { return nil, nil } diff --git a/pkg/plugin/networkx/mime_test.go b/pkg/plugin/networkx/mime_test.go index f99cd5a2..d9995d67 100644 --- a/pkg/plugin/networkx/mime_test.go +++ b/pkg/plugin/networkx/mime_test.go @@ -11,7 +11,7 @@ import ( func TestMarshalMIME(t *testing.T) { testCases := []struct { - whenPayload primitive.Object + whenPayload primitive.Value whenContentType string expectPayload []byte }{ @@ -68,7 +68,7 @@ func TestUnmarshalMIME(t *testing.T) { testCases := []struct { whenPayload []byte whenContentType string - expectPayload primitive.Object + expectPayload primitive.Value }{ { whenPayload: []byte(` diff --git a/pkg/plugin/networkx/router.go b/pkg/plugin/networkx/router.go index 49792e04..b3f95642 100644 --- a/pkg/plugin/networkx/router.go +++ b/pkg/plugin/networkx/router.go @@ -160,7 +160,7 @@ func (n *RouterNode) action(proc *process.Process, inPck *packet.Packet) ([]*pac if cur != nil { p := cur.methods[method] - var paramPairs []primitive.Object + var paramPairs []primitive.Value for i, v := range values { paramPairs = append(paramPairs, primitive.NewString(cur.paramNames[i])) paramPairs = append(paramPairs, primitive.NewString(v)) diff --git a/pkg/plugin/systemx/reflect.go b/pkg/plugin/systemx/reflect.go index be7924a1..06ac4ca4 100644 --- a/pkg/plugin/systemx/reflect.go +++ b/pkg/plugin/systemx/reflect.go @@ -255,7 +255,7 @@ func examplesToSpecs(examples []*primitive.Map) []scheme.Spec { return specs } -func specsToExamples(specs []scheme.Spec, batch bool) (primitive.Object, error) { +func specsToExamples(specs []scheme.Spec, batch bool) (primitive.Value, error) { if batch || len(specs) > 1 { return primitive.MarshalText(specs) } else { diff --git a/pkg/primitive/binary.go b/pkg/primitive/binary.go index e60bcf14..78fa6d5c 100644 --- a/pkg/primitive/binary.go +++ b/pkg/primitive/binary.go @@ -15,7 +15,7 @@ type ( Binary []byte ) -var _ Object = (Binary)(nil) +var _ Value = (Binary)(nil) // NewBinary returns a new Binary. func NewBinary(value []byte) Binary { @@ -42,7 +42,7 @@ func (o Binary) Kind() Kind { return KindBinary } -func (o Binary) Compare(v Object) int { +func (o Binary) Compare(v Value) int { if r, ok := v.(Binary); !ok { if o.Kind() > v.Kind() { return 1 @@ -59,8 +59,8 @@ func (o Binary) Interface() any { } // NewBinaryEncoder is encode byte like to Binary. -func NewBinaryEncoder() encoding2.Encoder[any, Object] { - return encoding2.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewBinaryEncoder() encoding2.Encoder[any, Value] { + return encoding2.EncoderFunc[any, Value](func(source any) (Value, error) { if s, ok := source.(encoding.BinaryMarshaler); ok { if data, err := s.MarshalBinary(); err != nil { return nil, err @@ -75,8 +75,8 @@ func NewBinaryEncoder() encoding2.Encoder[any, Object] { } // NewBinaryDecoder is decode Binary to byte like. -func NewBinaryDecoder() encoding2.Decoder[Object, any] { - return encoding2.DecoderFunc[Object, any](func(source Object, target any) error { +func NewBinaryDecoder() encoding2.Decoder[Value, any] { + return encoding2.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(Binary); ok { if t, ok := target.(encoding.BinaryUnmarshaler); ok { return t.UnmarshalBinary(s.Bytes()) diff --git a/pkg/primitive/bool.go b/pkg/primitive/bool.go index 4bfa38ac..7d41911f 100644 --- a/pkg/primitive/bool.go +++ b/pkg/primitive/bool.go @@ -12,7 +12,7 @@ type ( Bool bool ) -var _ Object = (Bool)(false) +var _ Value = (Bool)(false) var ( TRUE = NewBool(true) @@ -32,7 +32,7 @@ func (o Bool) Bool() bool { func (o Bool) Kind() Kind { return KindBool } -func (o Bool) Compare(v Object) int { +func (o Bool) Compare(v Value) int { if r, ok := v.(Bool); !ok { if o.Kind() > v.Kind() { return 1 @@ -53,8 +53,8 @@ func (o Bool) Interface() any { } // NewBoolEncoder is encode bool to Bool. -func NewBoolEncoder() encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewBoolEncoder() encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { if s := reflect.ValueOf(source); s.Kind() == reflect.Bool { return NewBool(s.Bool()), nil } @@ -63,8 +63,8 @@ func NewBoolEncoder() encoding.Encoder[any, Object] { } // NewBoolDecoder is decode Bool to bool. -func NewBoolDecoder() encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { +func NewBoolDecoder() encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(Bool); ok { if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { if t.Elem().Kind() == reflect.Bool { diff --git a/pkg/primitive/encoding.go b/pkg/primitive/encoding.go index 71f2f54b..04920c99 100644 --- a/pkg/primitive/encoding.go +++ b/pkg/primitive/encoding.go @@ -7,9 +7,9 @@ import ( ) var ( - textEncoder = encoding.NewEncoderGroup[any, Object]() - binaryEncoder = encoding.NewEncoderGroup[any, Object]() - decoder = encoding.NewDecoderGroup[Object, any]() + textEncoder = encoding.NewEncoderGroup[any, Value]() + binaryEncoder = encoding.NewEncoderGroup[any, Value]() + decoder = encoding.NewDecoderGroup[Value, any]() ) var ( @@ -52,16 +52,16 @@ func init() { } // MarshalText returns the Object of v. -func MarshalText(v any) (Object, error) { +func MarshalText(v any) (Value, error) { return textEncoder.Encode(v) } // MarshalBinary returns the Object of v. -func MarshalBinary(v any) (Object, error) { +func MarshalBinary(v any) (Value, error) { return binaryEncoder.Encode(v) } // Unmarshal parses the Object and stores the result. -func Unmarshal(data Object, v any) error { +func Unmarshal(data Value, v any) error { return decoder.Decode(data, v) } diff --git a/pkg/primitive/encoding_test.go b/pkg/primitive/encoding_test.go index 4b5ab241..c7b06e79 100644 --- a/pkg/primitive/encoding_test.go +++ b/pkg/primitive/encoding_test.go @@ -11,7 +11,7 @@ import ( func TestMarshalText(t *testing.T) { var testCase = []struct { when any - expect Object + expect Value }{ { when: nil, @@ -95,7 +95,7 @@ func TestMarshalText(t *testing.T) { func TestMarshalBinary(t *testing.T) { var testCase = []struct { when any - expect Object + expect Value }{ { when: nil, @@ -178,7 +178,7 @@ func TestMarshalBinary(t *testing.T) { func TestUnmarshal(t *testing.T) { var testCase = []struct { - when Object + when Value expect any }{ { diff --git a/pkg/primitive/float.go b/pkg/primitive/float.go index 5c3c3ebd..2f1416f6 100644 --- a/pkg/primitive/float.go +++ b/pkg/primitive/float.go @@ -9,7 +9,7 @@ import ( type ( Float interface { - Object + Value Float() float64 } // Float32 is a representation of a float64. @@ -35,7 +35,7 @@ func (o Float32) Kind() Kind { return KindFloat32 } -func (o Float32) Equal(v Object) bool { +func (o Float32) Equal(v Value) bool { if r, ok := v.(Float); !ok { if r, ok := v.(Integer); ok { return o.Float() == float64(r.Int()) @@ -49,7 +49,7 @@ func (o Float32) Equal(v Object) bool { } } -func (o Float32) Compare(v Object) int { +func (o Float32) Compare(v Value) int { if r, ok := v.(Float); !ok { if r, ok := v.(Integer); ok { return compare[float64](o.Float(), float64(r.Int())) @@ -83,7 +83,7 @@ func (o Float64) Kind() Kind { return KindFloat64 } -func (o Float64) Compare(v Object) int { +func (o Float64) Compare(v Value) int { if r, ok := v.(Float); !ok { if r, ok := v.(Integer); ok { return compare[float64](o.Float(), float64(r.Int())) @@ -104,8 +104,8 @@ func (o Float64) Interface() any { } // NewFloatEncoder is encode float to Float. -func NewFloatEncoder() encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewFloatEncoder() encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { if s := reflect.ValueOf(source); s.Kind() == reflect.Float32 { return NewFloat32(float32(s.Float())), nil } else if s := reflect.ValueOf(source); s.Kind() == reflect.Float64 { @@ -116,8 +116,8 @@ func NewFloatEncoder() encoding.Encoder[any, Object] { } // NewFloatDecoder is decode Float to float. -func NewFloatDecoder() encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { +func NewFloatDecoder() encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(Float); ok { if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { if t.Elem().Kind() == reflect.Float32 { diff --git a/pkg/primitive/int.go b/pkg/primitive/int.go index bf917c25..94371a82 100644 --- a/pkg/primitive/int.go +++ b/pkg/primitive/int.go @@ -9,7 +9,7 @@ import ( type ( Integer interface { - Object + Value Int() int64 } // Int is a representation of a int. @@ -44,7 +44,7 @@ func (o Int) Kind() Kind { return KindInt } -func (o Int) Compare(v Object) int { +func (o Int) Compare(v Value) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { return compare[int64](o.Int(), int64(r.Uint())) @@ -78,7 +78,7 @@ func (o Int8) Kind() Kind { return KindInt8 } -func (o Int8) Compare(v Object) int { +func (o Int8) Compare(v Value) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { return compare[int64](o.Int(), int64(r.Uint())) @@ -112,7 +112,7 @@ func (o Int16) Kind() Kind { return KindInt16 } -func (o Int16) Compare(v Object) int { +func (o Int16) Compare(v Value) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { return compare[int64](o.Int(), int64(r.Uint())) @@ -146,7 +146,7 @@ func (o Int32) Kind() Kind { return KindInt32 } -func (o Int32) Compare(v Object) int { +func (o Int32) Compare(v Value) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { return compare[int64](o.Int(), int64(r.Uint())) @@ -180,7 +180,7 @@ func (o Int64) Kind() Kind { return KindInt64 } -func (o Int64) Compare(v Object) int { +func (o Int64) Compare(v Value) int { if r, ok := v.(Integer); !ok { if r, ok := v.(Uinteger); ok { return compare[int64](o.Int(), int64(r.Uint())) @@ -201,8 +201,8 @@ func (o Int64) Interface() any { } // NewIntEncoder is encode int to Int. -func NewIntEncoder() encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewIntEncoder() encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { if s := reflect.ValueOf(source); s.Kind() == reflect.Int { return NewInt(int(s.Int())), nil } else if s := reflect.ValueOf(source); s.Kind() == reflect.Int8 { @@ -219,8 +219,8 @@ func NewIntEncoder() encoding.Encoder[any, Object] { } // NewIntDecoder is decode Int to int. -func NewIntDecoder() encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { +func NewIntDecoder() encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(Integer); ok { if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { if t.Elem().Kind() == reflect.Int { diff --git a/pkg/primitive/map.go b/pkg/primitive/map.go index fdde0247..8c9c3c0c 100644 --- a/pkg/primitive/map.go +++ b/pkg/primitive/map.go @@ -14,7 +14,7 @@ import ( type ( // Map is a representation of a map. Map struct { - value *immutable.SortedMap[Object, Object] + value *immutable.SortedMap[Value, Value] } mapTag struct { @@ -31,12 +31,12 @@ const ( tagMap = "map" ) -var _ Object = (*Map)(nil) -var _ immutable.Comparer[Object] = (*comparer)(nil) +var _ Value = (*Map)(nil) +var _ immutable.Comparer[Value] = (*comparer)(nil) // NewMap returns a new Map. -func NewMap(pairs ...Object) *Map { - b := immutable.NewSortedMapBuilder[Object, Object](&comparer{}) +func NewMap(pairs ...Value) *Map { + b := immutable.NewSortedMapBuilder[Value, Value](&comparer{}) for i := 0; i < len(pairs)/2; i++ { k := pairs[i*2] v := pairs[i*2+1] @@ -46,27 +46,27 @@ func NewMap(pairs ...Object) *Map { return &Map{value: b.Map()} } -func (o *Map) Get(key Object) (Object, bool) { +func (o *Map) Get(key Value) (Value, bool) { return o.value.Get(key) } -func (o *Map) GetOr(key, value Object) Object { +func (o *Map) GetOr(key, value Value) Value { if v, ok := o.Get(key); ok { return v } return value } -func (o *Map) Set(key, value Object) *Map { +func (o *Map) Set(key, value Value) *Map { return &Map{value: o.value.Set(key, value)} } -func (o *Map) Delete(key Object) *Map { +func (o *Map) Delete(key Value) *Map { return &Map{value: o.value.Delete(key)} } -func (o *Map) Keys() []Object { - var keys []Object +func (o *Map) Keys() []Value { + var keys []Value itr := o.value.Iterator() for !itr.Done() { @@ -105,7 +105,7 @@ func (o *Map) Kind() Kind { return KindMap } -func (o *Map) Compare(v Object) int { +func (o *Map) Compare(v Value) int { if r, ok := v.(*Map); !ok { if o.Kind() > v.Kind() { return 1 @@ -190,15 +190,15 @@ func (o *Map) Interface() any { return t.Interface() } -func (*comparer) Compare(a Object, b Object) int { +func (*comparer) Compare(a Value, b Value) int { return Compare(a, b) } // NewMapEncoder is encode map or struct to Map. -func NewMapEncoder(encoder encoding.Encoder[any, Object]) encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewMapEncoder(encoder encoding.Encoder[any, Value]) encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { if s := reflect.ValueOf(source); s.Kind() == reflect.Map { - pairs := make([]Object, len(s.MapKeys())*2) + pairs := make([]Value, len(s.MapKeys())*2) for i, k := range s.MapKeys() { if k, err := encoder.Encode(k.Interface()); err != nil { return nil, errors.WithMessage(err, fmt.Sprintf("key(%v) can't encode", k.Interface())) @@ -213,7 +213,7 @@ func NewMapEncoder(encoder encoding.Encoder[any, Object]) encoding.Encoder[any, } return NewMap(pairs...), nil } else if s := reflect.ValueOf(source); s.Kind() == reflect.Struct { - pairs := make([]Object, 0, s.NumField()*2) + pairs := make([]Value, 0, s.NumField()*2) for i := 0; i < s.NumField(); i++ { field := s.Type().Field(i) if !field.IsExported() { @@ -252,8 +252,8 @@ func NewMapEncoder(encoder encoding.Encoder[any, Object]) encoding.Encoder[any, } // NewMapDecoder is decode Map to map or struct. -func NewMapDecoder(decoder encoding.Decoder[Object, any]) encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { +func NewMapDecoder(decoder encoding.Decoder[Value, any]) encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(*Map); ok { if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { if t.Elem().Kind() == reflect.Map { diff --git a/pkg/primitive/object.go b/pkg/primitive/object.go deleted file mode 100644 index f216b51f..00000000 --- a/pkg/primitive/object.go +++ /dev/null @@ -1,53 +0,0 @@ -package primitive - -type ( - // Object is an atomic type. - Object interface { - Kind() Kind - Compare(v Object) int - Interface() any - } - - Kind uint -) - -const ( - KindInvalid Kind = iota - KindBinary - KindBool - KindInt - KindInt8 - KindInt16 - KindInt32 - KindInt64 - KindUint - KindUint8 - KindUint16 - KindUint32 - KindUint64 - KindFloat32 - KindFloat64 - KindMap - KindSlice - KindString -) - -func Compare(x, y Object) int { - if x == nil && y == nil { - return 0 - } else if x == nil { - return -1 - } else if y == nil { - return 1 - } else { - return x.Compare(y) - } -} - -func Interface(v Object) any { - if v == nil { - return nil - } else { - return v.Interface() - } -} diff --git a/pkg/primitive/pick.go b/pkg/primitive/pick.go index e9d942df..238ea5ee 100644 --- a/pkg/primitive/pick.go +++ b/pkg/primitive/pick.go @@ -4,7 +4,7 @@ import ( "strconv" ) -func Pick[T any](v Object, paths ...string) (T, bool) { +func Pick[T any](v Value, paths ...string) (T, bool) { var zero T cur := v diff --git a/pkg/primitive/pointer.go b/pkg/primitive/pointer.go index b7d10716..0cae7ef7 100644 --- a/pkg/primitive/pointer.go +++ b/pkg/primitive/pointer.go @@ -8,8 +8,8 @@ import ( ) // NewPointerEncoder is encode *T to T. -func NewPointerEncoder(encoder encoding.Encoder[any, Object]) encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewPointerEncoder(encoder encoding.Encoder[any, Value]) encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { if source == nil { return nil, nil } @@ -21,8 +21,8 @@ func NewPointerEncoder(encoder encoding.Encoder[any, Object]) encoding.Encoder[a } // NewPointerDecoder is decode T to *T. -func NewPointerDecoder(decoder encoding.Decoder[Object, any]) encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { +func NewPointerDecoder(decoder encoding.Decoder[Value, any]) encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if source == nil { return nil } diff --git a/pkg/primitive/shortcut.go b/pkg/primitive/shortcut.go index 3f4b172f..83444a8d 100644 --- a/pkg/primitive/shortcut.go +++ b/pkg/primitive/shortcut.go @@ -6,9 +6,9 @@ import ( ) // NewPointerEncoder is encode Object to Object. -func NewShortcutEncoder() encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { - if s, ok := source.(Object); ok { +func NewShortcutEncoder() encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { + if s, ok := source.(Value); ok { return s, nil } return nil, errors.WithStack(encoding.ErrUnsupportedValue) @@ -16,9 +16,9 @@ func NewShortcutEncoder() encoding.Encoder[any, Object] { } // NewShortcutDecoder is decode Object to Object. -func NewShortcutDecoder() encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { - if t, ok := target.(*Object); ok { +func NewShortcutDecoder() encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { + if t, ok := target.(*Value); ok { *t = source return nil } diff --git a/pkg/primitive/slice.go b/pkg/primitive/slice.go index 0fbfb386..406b9447 100644 --- a/pkg/primitive/slice.go +++ b/pkg/primitive/slice.go @@ -12,26 +12,26 @@ import ( type ( // Slice is a representation of a slice. Slice struct { - value *immutable.List[Object] + value *immutable.List[Value] } ) -var _ Object = (*Slice)(nil) +var _ Value = (*Slice)(nil) // NewSlice returns a new Slice. -func NewSlice(values ...Object) *Slice { - b := immutable.NewListBuilder[Object]() +func NewSlice(values ...Value) *Slice { + b := immutable.NewListBuilder[Value]() for _, v := range values { b.Append(v) } return &Slice{value: b.List()} } -func (o *Slice) Prepend(value Object) *Slice { +func (o *Slice) Prepend(value Value) *Slice { return &Slice{value: o.value.Prepend(value)} } -func (o *Slice) Append(value Object) *Slice { +func (o *Slice) Append(value Value) *Slice { return &Slice{value: o.value.Append(value)} } @@ -39,14 +39,14 @@ func (o *Slice) Sub(start, end int) *Slice { return &Slice{value: o.value.Slice(start, end)} } -func (o *Slice) Get(index int) Object { +func (o *Slice) Get(index int) Value { if index >= o.value.Len() { return nil } return o.value.Get(index) } -func (o *Slice) Set(index int, value Object) *Slice { +func (o *Slice) Set(index int, value Value) *Slice { if index < 0 && index >= o.value.Len() { return o } @@ -78,7 +78,7 @@ func (o *Slice) Kind() Kind { return KindSlice } -func (o *Slice) Compare(v Object) int { +func (o *Slice) Compare(v Value) int { if r, ok := v.(*Slice); !ok { if o.Kind() > v.Kind() { return 1 @@ -134,10 +134,10 @@ func (o *Slice) Interface() any { } // NewSliceEncoder is encode slice or array to Slice. -func NewSliceEncoder(encoder encoding.Encoder[any, Object]) encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewSliceEncoder(encoder encoding.Encoder[any, Value]) encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { if s := reflect.ValueOf(source); s.Kind() == reflect.Slice || s.Kind() == reflect.Array { - values := make([]Object, s.Len()) + values := make([]Value, s.Len()) for i := 0; i < s.Len(); i++ { if v, err := encoder.Encode(s.Index(i).Interface()); err != nil { return nil, err @@ -152,8 +152,8 @@ func NewSliceEncoder(encoder encoding.Encoder[any, Object]) encoding.Encoder[any } // NewSliceDecoder is decode Slice to slice or array. -func NewSliceDecoder(decoder encoding.Decoder[Object, any]) encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { +func NewSliceDecoder(decoder encoding.Decoder[Value, any]) encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(*Slice); ok { if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { if t.Elem().Kind() == reflect.Slice || t.Elem().Kind() == reflect.Array { diff --git a/pkg/primitive/string.go b/pkg/primitive/string.go index 4492954c..51f52c4c 100644 --- a/pkg/primitive/string.go +++ b/pkg/primitive/string.go @@ -13,7 +13,7 @@ type ( String string ) -var _ Object = (String)("") +var _ Value = (String)("") // NewString returns a new String. func NewString(value string) String { @@ -40,7 +40,7 @@ func (o String) Kind() Kind { return KindString } -func (o String) Compare(v Object) int { +func (o String) Compare(v Value) int { if r, ok := v.(String); !ok { if o.Kind() > v.Kind() { return 1 @@ -57,8 +57,8 @@ func (o String) Interface() any { } // NewStringEncoder is encode string to String. -func NewStringEncoder() encoding2.Encoder[any, Object] { - return encoding2.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewStringEncoder() encoding2.Encoder[any, Value] { + return encoding2.EncoderFunc[any, Value](func(source any) (Value, error) { if s, ok := source.(encoding.TextMarshaler); ok { if text, err := s.MarshalText(); err != nil { return nil, err @@ -73,8 +73,8 @@ func NewStringEncoder() encoding2.Encoder[any, Object] { } // NewStringDecoder is decode String to string. -func NewStringDecoder() encoding2.Decoder[Object, any] { - return encoding2.DecoderFunc[Object, any](func(source Object, target any) error { +func NewStringDecoder() encoding2.Decoder[Value, any] { + return encoding2.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(String); ok { if t, ok := target.(encoding.TextUnmarshaler); ok { return t.UnmarshalText([]byte(s.String())) diff --git a/pkg/primitive/uint.go b/pkg/primitive/uint.go index fc98dbe2..9c8b43fb 100644 --- a/pkg/primitive/uint.go +++ b/pkg/primitive/uint.go @@ -9,7 +9,7 @@ import ( type ( Uinteger interface { - Object + Value Uint() uint64 } // Uint is a representation of a uint. @@ -44,7 +44,7 @@ func (o Uint) Kind() Kind { return KindUint } -func (o Uint) Compare(v Object) int { +func (o Uint) Compare(v Value) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { return compare[int64](int64(o.Uint()), r.Int()) @@ -78,7 +78,7 @@ func (o Uint8) Kind() Kind { return KindUint8 } -func (o Uint8) Compare(v Object) int { +func (o Uint8) Compare(v Value) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { return compare[int64](int64(o.Uint()), r.Int()) @@ -112,7 +112,7 @@ func (o Uint16) Kind() Kind { return KindUint16 } -func (o Uint16) Compare(v Object) int { +func (o Uint16) Compare(v Value) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { return compare[int64](int64(o.Uint()), r.Int()) @@ -146,7 +146,7 @@ func (o Uint32) Kind() Kind { return KindUint32 } -func (o Uint32) Compare(v Object) int { +func (o Uint32) Compare(v Value) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { return compare[int64](int64(o.Uint()), r.Int()) @@ -180,7 +180,7 @@ func (o Uint64) Kind() Kind { return KindUint64 } -func (o Uint64) Compare(v Object) int { +func (o Uint64) Compare(v Value) int { if r, ok := v.(Uinteger); !ok { if r, ok := v.(Integer); ok { return compare[int64](int64(o.Uint()), r.Int()) @@ -201,8 +201,8 @@ func (o Uint64) Interface() any { } // NewUintEncoder is encode uint to Uint. -func NewUintEncoder() encoding.Encoder[any, Object] { - return encoding.EncoderFunc[any, Object](func(source any) (Object, error) { +func NewUintEncoder() encoding.Encoder[any, Value] { + return encoding.EncoderFunc[any, Value](func(source any) (Value, error) { if s := reflect.ValueOf(source); s.Kind() == reflect.Uint { return NewUint(uint(s.Uint())), nil } else if s := reflect.ValueOf(source); s.Kind() == reflect.Uint8 { @@ -219,8 +219,8 @@ func NewUintEncoder() encoding.Encoder[any, Object] { } // NewUintDecoder is decode Uint to uint. -func NewUintDecoder() encoding.Decoder[Object, any] { - return encoding.DecoderFunc[Object, any](func(source Object, target any) error { +func NewUintDecoder() encoding.Decoder[Value, any] { + return encoding.DecoderFunc[Value, any](func(source Value, target any) error { if s, ok := source.(Uinteger); ok { if t := reflect.ValueOf(target); t.Kind() == reflect.Pointer { if t.Elem().Kind() == reflect.Uint { diff --git a/pkg/primitive/value.go b/pkg/primitive/value.go new file mode 100644 index 00000000..87eb120c --- /dev/null +++ b/pkg/primitive/value.go @@ -0,0 +1,57 @@ +package primitive + +// Kind represents the enumeration of data types. +type Kind uint + +// Value is an interface that signifies atomic data types. +type Value interface { + Kind() Kind // Returns the type of data. + Compare(v Value) int // Compares with another Value and returns the order. + Interface() any // Converts the internal value to a generic interface. +} + +// Constants representing various data types. +const ( + KindInvalid Kind = iota + KindBinary + KindBool + KindInt + KindInt8 + KindInt16 + KindInt32 + KindInt64 + KindUint + KindUint8 + KindUint16 + KindUint32 + KindUint64 + KindFloat32 + KindFloat64 + KindMap + KindSlice + KindString +) + +// Compare function compares two Values and returns their order. +// Nil values are treated as the lowest order. +func Compare(x, y Value) int { + if x == nil && y == nil { + return 0 + } else if x == nil { + return -1 + } else if y == nil { + return 1 + } else { + return x.Compare(y) + } +} + +// Interface function converts a Value to a generic interface. +// Nil values are returned as a nil interface. +func Interface(v Value) any { + if v == nil { + return nil + } else { + return v.Interface() + } +}