V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  Weny  ›  全部回复第 4 页 / 共 117 页
回复总数  2325
1  2  3  4  5  6  7  8  9  10 ... 117  
2021-10-24 17:16:00 +08:00
回复了 balabalaguguji 创建的主题 Go 编程语言 看到 Go 与 MongoDB 的交互方式,我想放弃 Go 了
@balabalaguguji 其实本质上就是一个 Map 的 builder

第一个问题 :queryBuilder.Where("user_id",id)

aggregate 我觉得再封装一个 pipeline builder ,再扩展一下已有的 queryBuilder 比如
pipelineBuilder(
queryBuilder.Match("taskId",func(q){return q.in(taskIds)}),
queryBuilder.Lookup(func(q){return q.Form("")...})
...
)
2021-10-24 15:09:54 +08:00
回复了 balabalaguguji 创建的主题 Go 编程语言 看到 Go 与 MongoDB 的交互方式,我想放弃 Go 了
简单的 filter 可以自己封装一下,几百行代码的事情。
```GO
import (
"context"
"fmt"
"strings"
)

func initQuery() *Query {
q := Query{
operation: "",
options: nil,
conditions: make(M),
field: "",
}
return &q
}

func NewQuery(fn ...func(query *Query)) *Query {
q := Query{
operation: "",
options: &Options{
skip: 0,
projection: make(M),
},
conditions: make(M),
field: "",
}
if len(fn) > 0 {
for _, f := range fn {
f(&q)
}
}
return &q
}

func SetConn(c Connection) func(query *Query) {
return func(query *Query) {
query.connection = c
}
}

func NewQueryWitConn(c Connection) *Query {
return NewQuery(SetConn(c))
}

type Query struct {
connection Connection
operation string
options *Options
conditions M
field string
model interface{}
}

func (q *Query) GetConditions() M {
return q.conditions
}

func (q *Query) GetSelect() M {
return q.options.projection
}

func mappingStringToFieldSets(value Input, projection bool) Input {
out := -1
if projection {
out = 0
}
obj := make(M)
switch value.(type) {
case string:
strArray := strings.Fields(strings.TrimSpace(value.(string)))
for _, v := range strArray {
if v[0] == '-' {
v = v[1:]
obj[v] = out
} else {
obj[v] = 1
}
}
case M:
obj = value.(M)
}
return obj
}

func (q *Query) Sort(value interface{}) *Query {
q.options.sort = mappingStringToFieldSets(value, false).(M)
return q
}

func (q *Query) Select(value interface{}) *Query {
q.options.projection = mappingStringToFieldSets(value, true).(M)
return q
}

func (q *Query) Skip(value int32) *Query {
q.options.skip = value
return q
}

func (q *Query) Where(args ...interface{}) *Query {
//q.field = field
switch len(args) {
// first args is string
case 1:
if field, ok := args[0].(string); ok {
q.Set(field)
}
// Where("version",1) where version is equals q
case 2:
if field, ok := args[0].(string); ok {
q.Set(field).Eq(args[1])
}
// Where("version",">=",1) gte 1
case 3:
if field, ok := args[0].(string); ok {
q.Set(field)
}
if operators, ok := args[1].(string); ok {
q.bindCondition(
chain(
getFlagWrapperFromString(operators),
inputBuilder,
)(args[2]),
)
}
}
return q
}

func (q *Query) Set(field string) *Query {
q.field = field
return q
}

func (q *Query) Eq(input interface{}) *Query {
q.
ensureField("Eq").
bindCondition(
chain(
inputLogger,
inputBuilder,
)(input),
).
resetField()
return q
}

func (q *Query) Equals(input interface{}) *Query {
q.
ensureField("Equals").
bindCondition(
chain(inputBuilder)(input),
).
resetField()
return q
}

func (q *Query) AutoBindConditions(flag string, condition interface{}) *Query {
if q.hasField() {
q.bindCondition(
chain(
inputBuilder,
)(condition),
).resetField()
} else {
q.bindCondition(
chain(
inputWrapper(flag),
inputBuilder,
)(condition),
).resetField()
}
return q
}

/*
e.g. query.Or([]M{{"name": "weny"}, {"age": "20"}})
*/
func (q *Query) Or(value interface{}) *Query {
flag := "$or"
return q.AutoBindConditions(flag, value)
}

/*
e.g. query.Nor([]M{{"name": "weny"}, {"age": "20"}})
*/
func (q *Query) Nor(value interface{}) *Query {
flag := "$nor"
return q.AutoBindConditions(flag, value)
}

func (q *Query) And(value interface{}) *Query {
flag := "$and"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Not(value interface{}) *Query {
flag := "$not"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Gt(value interface{}) *Query {
flag := "$gt"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Gte(value interface{}) *Query {
flag := "$gte"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Lt(value interface{}) *Query {
flag := "$lt"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Lte(value interface{}) *Query {
flag := "$lte"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Ne(value interface{}) *Query {
flag := "$ne"
return q.AutoBindConditions(flag, value)
}

func (q *Query) In(value interface{}) *Query {
flag := "$in"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Nin(value interface{}) *Query {
flag := "$nin"
return q.AutoBindConditions(flag, value)
}

func (q *Query) All(value interface{}) *Query {
flag := "$all"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Regex(value interface{}) *Query {
flag := "$regex"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Size(value interface{}) *Query {
flag := "$size"
return q.AutoBindConditions(flag, value)
}

func (q *Query) MaxDistance(value interface{}) *Query {
flag := "$maxDistance"
return q.AutoBindConditions(flag, value)
}

func (q *Query) MinDistance(value interface{}) *Query {
flag := "$minDistance"
return q.AutoBindConditions(flag, value)
}

func (q *Query) Mod(value interface{}) *Query {
flag := "$mod"
return q.AutoBindConditions(flag, value)
}

//TODO: geometry

func (q *Query) Exists(value bool) *Query {
flag := "$exists"
return q.AutoBindConditions(flag, value)
}

func (q *Query) ElemMatch(value interface{}) *Query {
flag := "$elemMatch"
return q.AutoBindConditions(flag, value)
}

func (q *Query) bindCondition(value Input) *Query {
q.conditions[q.field] = value
return q
}

func (q *Query) resetField() *Query {
q.field = ""
return q
}

func (q *Query) setField(field string) *Query {
q.field = field
return q
}
func (q *Query) hasField() bool {
if q.field == "" {
return false
}
return true
}

func (q *Query) ensureField(method string) *Query {
if q.field == "" {
panic(method + " must be used after Where() ")
}
return q
}

func inputLogger(input Input) Input {
go func() {
fmt.Print(input)
}()
return input
}

func inputWrapper(flag string) InputEndpoint {
return func(input Input) Input {
return M{flag: input}
}
}
func getFlagWrapperFromString(arg string) InputEndpoint {
switch arg {
case "<":
return inputWrapper("$lt")
case ">":
return inputWrapper("$gt")
case "=":
return inputWrapper("$eq")
case ">=":
return inputWrapper("$gte")
case "<=":
return inputWrapper("$lte")
default:
return inputWrapper("$eq")
}
}

func inputBuilder(input Input) Input {
var res interface{}
switch input.(type) {
case func(q *Query):
query := NewQuery()
input.(func(q *Query))(query)
res = query.conditions
break
case func(q *Query) *Query:
res = input.(func(q *Query) *Query)(initQuery()).conditions
break
case interface{}:
res = input
break
}
return res
}
```
会的。显示器这类盒子保存好。 有包装盒连 iMac 空运都可以哈哈哈哈(狗头
2021-10-21 08:37:53 +08:00
回复了 DosLee 创建的主题 MacBook Pro 你们的新 Mac 什么时候到货, 包括国内国外的
定制版本 15 号-22 号,比国内同一天定的要晚 4 天...
2021-10-19 07:43:52 +08:00
回复了 qdwang 创建的主题 Apple 新 macbook pro 好重
健身卡没白办 (狗头
2021-10-18 07:36:41 +08:00
回复了 songdezu 创建的主题 问与答 React 现在什么 state 管理库最成熟稳定好用啊?
zustand
2021-10-17 15:10:20 +08:00
回复了 levineet 创建的主题  WATCH Apple Watch Hermès Series 7 开箱
@levineet 大象灰应该就是官网的亚麻蓝? 这个颜色确实好看,年轻一点😋
2021-10-17 14:37:53 +08:00
回复了 levineet 创建的主题  WATCH Apple Watch Hermès Series 7 开箱
我定了蓝色的 要等到 27 号😢
+1 屏幕贴着大腿 误触后耳机声音突然被调大 吓死
13 应该还好一点? pro 双十一下单估计双十二到货😂
2021-10-13 13:00:22 +08:00
回复了 WanzizZ 创建的主题  WATCH 奇怪了,昨晚怎么没有 Apple Watch 7 的媒体评测出来?
首发抢了 aw,但是最快要 27 才送达… 完全不止不住渴😂😂 朋友 17 号定的 pro 14 号才到。感觉无望提前发货了
2021-10-13 10:19:25 +08:00
回复了 WanzizZ 创建的主题  WATCH 奇怪了,昨晚怎么没有 Apple Watch 7 的媒体评测出来?
好像今天晚上 9 点之后
2021-10-11 15:43:33 +08:00
回复了 xinhaiw 创建的主题 Apple m1x 顶配 32g 2tb?
@neptuno 手动 doge
2021-10-10 17:13:44 +08:00
回复了 xinhaiw 创建的主题 Apple m1x 顶配 32g 2tb?
虽然拉到 64 会心疼,总觉得 32 过两年又要掏钱了😂
2021-10-02 14:38:49 +08:00
回复了 13936 创建的主题 硬件 显示器破成这样了还在用,是真爱吗?
是拿原包装邮寄的吗?不过说起来,也确实感觉 u2720 的保包装比之前减配了
2021-09-03 12:00:37 +08:00
回复了 HuPu 创建的主题 硬件 什么笔记本像 mac 一样续航安静有 bigger
xps 散热一般… 新模具不知道有没有改善
2021-08-27 16:24:00 +08:00
回复了 chencc48111 创建的主题 分享发现 Onenote 几个不能忍的点
Mac 和 iPad 公式一多就卡出翔
已投,请查收
2021-08-14 10:02:29 +08:00
回复了 zycz2p 创建的主题 分享创造 女朋友给的七夕礼物~分享一下,嘿嘿。
幸好我不识字
1  2  3  4  5  6  7  8  9  10 ... 117  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2476 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 48ms · UTC 01:10 · PVG 09:10 · LAX 18:10 · JFK 21:10
Developed with CodeLauncher
♥ Do have faith in what you're doing.