123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- package yu_proto
- import (
- yu_strconv "gogs.qqck.cn/s/tools/strconv"
- )
- func (t *Protobuf) SetBool(value bool, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetByte(value byte, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetInt8(value int8, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetUint8(value uint8, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetInt16(value int16, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetUint16(value uint16, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetInt32(value int32, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetUint32(value uint32, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetInt64(value int64, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetUint64(value uint64, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetFloat32(value float32, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetFloat64(value float64, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- t.set(value, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetString(value string, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- j_buf := make([]byte, len(value))
- copy(j_buf, value)
- t.set(j_buf, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) SetBytes(value []byte, ids ...any) *Protobuf {
- if len(ids) == 0 {
- return t
- }
- j_buf := make([]byte, len(value))
- copy(j_buf, value)
- t.set(j_buf, ids, 0, nil, t.list)
- return t
- }
- func (t *Protobuf) set(value any, ids []any, idi int, parent *node, list *node) {
- // 判断是否会有子链表
- if j_idi := idi + 1; j_idi < len(ids) {
- var j_i int
- switch j_id := ids[idi].(type) {
- case string:
- if j_id := yu_strconv.ParseUint64(j_id); j_id < 1 {
- return
- } else {
- var j_prev *node
- for {
- // 寻找同id节点
- if list.id == j_id {
- switch list.value.(type) {
- case *node:
- // 是链表结构,无需操作
- t.set(value, ids, j_idi, list, list.value.(*node))
- case *[]*node:
- // 是数组结构,无需操作
- t.set(value, ids, j_idi, list, nil)
- default:
- // 非链表结构,改变其值为链表
- list.value = &node{}
- t.set(value, ids, j_idi, list, list.value.(*node))
- }
- return
- }
- // 判断是否插入节点
- if list.id > j_id {
- // 判断是否为首节点
- if j_prev == nil {
- list._next = &node{list._next, list.id, list.value}
- list.id, list.value = j_id, &node{}
- t.set(value, ids, j_idi, list, list.value.(*node))
- } else {
- j_prev._next = &node{list, j_id, &node{}}
- t.set(value, ids, j_idi, j_prev._next, j_prev._next.value.(*node))
- }
- return
- }
- // 判断是否到链表尾部
- if list._next == nil {
- // 判断当前节点是否为空节点
- if list.id == 0 {
- // 空节点就直接赋值
- list.id, list.value = j_id, &node{}
- t.set(value, ids, j_idi, list, list.value.(*node))
- } else {
- // 创建新节点
- list._next = &node{nil, j_id, &node{}}
- t.set(value, ids, j_idi, list._next, list._next.value.(*node))
- }
- return
- }
- j_prev, list = list, list._next
- }
- }
- case int8:
- j_i = int(j_id)
- case uint8:
- j_i = int(j_id)
- case int16:
- j_i = int(j_id)
- case uint16:
- j_i = int(j_id)
- case int32:
- j_i = int(j_id)
- case uint32:
- j_i = int(j_id)
- case int64:
- j_i = int(j_id)
- case uint64:
- j_i = int(j_id)
- case int:
- j_i = j_id
- case uint:
- j_i = int(j_id)
- case uintptr:
- j_i = int(j_id)
- default:
- return
- }
- if j_i < 0 {
- return
- }
- if parent == nil {
- // 数组必须有一个父节点
- return
- }
- var j_array *[]*node
- switch parent.value.(type) {
- case *[]*node:
- // 是数组结构
- j_array = parent.value.(*[]*node)
- // 数组容量不足索引时扩充数组
- for len(*j_array) < j_i+1 {
- *j_array = append(*j_array, nil)
- }
- default:
- // 非数组结构,改变其值为数组
- j_tmp := make([]*node, j_i+1)
- j_array, parent.value = &j_tmp, &j_tmp
- }
- if (*j_array)[j_i] == nil {
- (*j_array)[j_i] = &node{}
- }
- if (*j_array)[j_i].value == nil {
- (*j_array)[j_i]._next, (*j_array)[j_i].id, (*j_array)[j_i].value = nil, 0, &node{}
- }
- switch (*j_array)[j_i].value.(type) {
- case *node:
- // 是链表结构,无需操作
- default:
- (*j_array)[j_i]._next, (*j_array)[j_i].id, (*j_array)[j_i].value = nil, 0, &node{}
- }
- t.set(value, ids, j_idi, (*j_array)[j_i], (*j_array)[j_i].value.(*node))
- return
- }
- // 最后一层链表
- var j_i int
- switch j_id := ids[idi].(type) {
- case string:
- if j_id := yu_strconv.ParseUint64(j_id); j_id < 1 {
- return
- } else {
- var j_prev *node
- for {
- // 寻找同id节点
- if list.id == j_id {
- list.value = value
- return
- }
- // 判断是否插入节点
- if list.id > j_id {
- // 判断是否为首节点
- if j_prev == nil {
- list._next = &node{list._next, list.id, list.value}
- list.id, list.value = j_id, value
- } else {
- j_prev._next = &node{list, j_id, value}
- }
- return
- }
- // 判断是否到链表尾部
- if list._next == nil {
- // 判断当前节点是否为空节点
- if list.id == 0 {
- // 空节点就直接赋值
- list.id, list.value = j_id, value
- } else {
- // 创建新节点
- list._next = &node{nil, j_id, value}
- }
- return
- }
- j_prev, list = list, list._next
- }
- }
- case int8:
- j_i = int(j_id)
- case uint8:
- j_i = int(j_id)
- case int16:
- j_i = int(j_id)
- case uint16:
- j_i = int(j_id)
- case int32:
- j_i = int(j_id)
- case uint32:
- j_i = int(j_id)
- case int64:
- j_i = int(j_id)
- case uint64:
- j_i = int(j_id)
- case int:
- j_i = j_id
- case uint:
- j_i = int(j_id)
- case uintptr:
- j_i = int(j_id)
- default:
- return
- }
- if j_i < 0 {
- return
- }
- if parent == nil {
- // 数组必须有一个父节点
- return
- }
- var j_array *[]*node
- switch parent.value.(type) {
- case *[]*node:
- // 是数组结构
- j_array = parent.value.(*[]*node)
- // 数组容量不足索引时扩充数组
- for len(*j_array) < j_i+1 {
- *j_array = append(*j_array, nil)
- }
- default:
- // 非数组结构,改变其值为数组
- j_tmp := make([]*node, j_i+1)
- j_array, parent.value = &j_tmp, &j_tmp
- }
- if (*j_array)[j_i] == nil {
- (*j_array)[j_i] = &node{}
- }
- (*j_array)[j_i]._next, (*j_array)[j_i].id, (*j_array)[j_i].value = nil, 0, value
- return
- }
|