Collection V1.3.1 upgrade record

Time:2021-2-15

Collection V1.3.1 upgrade record

Project address:github.com/jianfengye/collectionWelcome to star.

Collection Manual address:collection.funaio.cn/

The collection library is upgraded to V1.3.1.

From v1.2.0 to V1.3.1, the following changes have been made:

  • Transformation of instruction document into online manual
  • Objpointcollection structure is added
  • The toobjs method is added
  • Refactoring absarray
  • Containscount method is added
  • The errors library is replaced with github.com/pkg/errors library

Transformation of instruction document into online manual

This is inspired by the front-end students in the group. They like to use vuepress to write document manuals. So I put the previous library manual, a big file README.md It has been modified into “guide” and “manual” under the doc directory. Every method in the manual is a markdown.

My thinking is to split the description of the method in this way, and extend some examples in the subsequent manual. There are many advantages to using vuepress. One is to build a manual address on your own alicloud server:collection.funaio.cn/. This address makes it easier for me to view it in the development process, and I don’t need to go to GitHub to see it. After all, GitHub has not been able to log in so well recently.

Collection V1.3.1 upgrade record

Here, you can download vuepress from Amway. It’s a good tool to convert markdown to HTML.

Objpointcollection structure is added

This requirement comes from the requirements encountered in business development. An array of pointer objects. We want the pointer object array to use all the methods of the colleciton library as well. So this method is added.

type FooBar struct {
    Foo string
    Bar int
}

func FooBarCompare(a interface{}, b interface{}) int {
    aobj := a.(*FooBar)
    bobj := b.(*FooBar)
    return aobj.Bar - bobj.Bar
}

func InitFooObjPoints() []*FooBar {
    return []*FooBar{
        {
            Foo: "astring",
            Bar: 1,
        },
        {
            Foo: "bstring",
            Bar: 2,
        },
    }
}

func TestObjPointCollection_Normal(t *testing.T) {
    objs := InitFooObjPoints()
    coll := NewObjPointCollection(objs).SetCompare(FooBarCompare)

    //[append] (#append) mounts an element to the current collection
    {
        count := coll.Copy().Append(&FooBar{
            Foo: "cstring",
            Bar: 3,
        }).Count()
        if count != 3 {
            t.Fatal("append error")
        }
    }

    //[contain] (#contain) determines whether an element is in the collection
    {
        obj := objs[0]
        if coll.Contains(obj) != true {
            t.Fatal("contains error")
        }
    }

    //[copy] (#copy) creates an array of the same type according to the current array
    {
        if coll.Copy().Count() != 2 {
            t.Fatal("copy error")
        }
    }
}

The toobjs method is added

This method is designed for objcollection and objpointcollection. If we want to restore the collection to an object array or an object pointer array, we can use this method. This method uses reflection.


func TestObjPointCollection_ToObjs(t *testing.T) {
    a1 := &Foo{A: "a1", B: 1}
    a2 := &Foo{A: "a2", B: 2}
    a3 := &Foo{A: "a3", B: 3}

    bArr := []*Foo{}
    objColl := NewObjPointCollection([]*Foo{a1, a2, a3})
    err := objColl.ToObjs(&bArr)
    if err != nil {
        t.Fatal(err)
    }
    if len(bArr) != 3 {
        t.Fatal("toObjs error len")
    }
    if bArr[1].A != "a2" {
        t.Fatal("toObjs error copy")
    }
}

Refactoring absarray

The previous onecollection.funaio.cn/guide/introduc…I talked about my thoughts on designing the collection library at that time. But in version 1.3.1, I think the thinking of implementation is not clear enough. My transformation this time includes storing the type of the upper collection in the bottom absarray.

const (
    TYPE_UNKNWON EleType = iota
    Type_INT
    Type_INT64
    Type_INT32
    TYPE_STRING
    TYPE_FLOAT32
    TYPE_FLOAT64
    TYPE_OBJ
    TYPE_OBJ_POINT
)

Then we implement the defense method of must internally

mustSetCompare
mustBeNumType
mustBeBaseType
mustNotBeBaseType
mustNotBeEmpty

Finally, defense judgment is made before each specific implementation method. In this way, the readability of the whole code will be improved.

Add containscount method

This method is also mentioned in the process of using. We want to judge not only whether an element is in the array, but also how many times the element appears in the array. So there is this method.

func TestAbsCollection_ContainsCount(t *testing.T) {
    intColl := NewIntCollection([]int{1, 2, 2, 3})
    count := intColl.ContainsCount(2)
    if count != 2 {
        t.Fatal(errors.New("contains count error"))
    }
}

Replace errors with github.com/pkg/errors

The advantages of replacing the official errors library with PKG / errors library will not be described here. Those who are interested can refer to itwww.bilibili.com/video/BV1hE411c7Z…

summary

Recently, there is a new module service inside using the collection library for business development, which speeds up a lot of development speed.

Have a good time.

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]