首页 > 编程语言 >MagicArray:像php一样,让Go业务代码不再卷!

MagicArray:像php一样,让Go业务代码不再卷!

时间:2023-12-26 20:55:38浏览次数:39  
标签:arr string err usersArr json MagicArray Go php Id

卷!一个字形容了当今码农行业的现状。一层一层的代码结构,一个一个dto,entity,vo,req,resp。这些对象越来越多,让码农们非常劳于奔命,加一个字段,改一个字段,影响面有多少。代码量也越来越大。有可能一个代码,要建10多个数据对象。这虽然有点夸张,但体现了行业的乱象。

我是曾经写php代码的研发,由于php的并发能力的不足,和编译检错能力不足,导致行业用php越来越少了。但是不得不说,在php的时代,跟本不需要这么多对象,因为有一个强大的array,包罗万象。难度我们go、java都不能也通过这种方式实现吗?

Allright,  由此,MagicArray就诞生了,不用关心类型,不用关心空指针,不用创建哪么多数据对象,轻松数据转换。废话不多说,上代码:

DTO替代方式:

package main

import (
 "encoding/json"
 "fmt"
 arr "github.com/lingdor/magicarray"
 "time"
)

type UserDTO struct {
 Id   int `json:"userid"`
 Name string
}

type ScoreDTO struct {
 Score     int
 ScoreTime time.Time
}

type AreaDto struct {
 CityId int
 City   string
}

func dtosCommand() {

 user := UserDTO{
  Id:   1,
  Name: "bobby",
 }
 score := ScoreDTO{
  Score:     66,
  ScoreTime: time.Now(),
 }
 area := AreaDto{
  CityId: 10000,
  City:   "newyork",
 }

 mix, _ := arr.Merge(arr.ValueofStruct(user), score, area)
 mix = arr.Gets(mix, "Id", "City", "Score")
 if bs, err := json.Marshal(mix); err == nil {
  fmt.Println(string(bs))
 } else {
  panic(err)
 }

}

输出

{"userid":1,"City":"newyork","Score":66}

数据变换

package main

import (
 "encoding/json"
 "fmt"
 arr "github.com/lingdor/magicarray"
)

type ColumnUserEntity struct {
 Id       int `json:"uid"`
 UserName string
 IsMale   bool
}

func columnCommand() {

 users := []ColumnUserEntity{
  {
   Id:       1,
   UserName: "Bobby",
   IsMale:   true,
  },
  {
   Id:       2,
   UserName: "Lily",
   IsMale:   false,
  },
 }

 usersArr := arr.ValueOfSlice(users)
 usersArr = arr.WashColumnTag(usersArr, arr.WashTagJsonInitalLowerOpt())
 if bs, err := json.Marshal(usersArr); err == nil {
  fmt.Println(string(bs))
 } else {
  panic(err)
 }

 usersArr = arr.Column(usersArr, "UserName")
 if bs, err := json.Marshal(usersArr); err == nil {
  fmt.Println(string(bs))
 } else {
  panic(err)
 }

}

输出

["Bobby","Lily"]

tag操作

package main

import (
 "encoding/json"
 "fmt"
 arr "github.com/lingdor/magicarray"
)

type UserEntity struct {
 Id       int `json:"uid"`
 UserName string
 IsMale   bool
}

func tagCommand() {

 users := []UserEntity{
  {
   Id:       1,
   UserName: "Bobby",
   IsMale:   true,
  },
  {
   Id:       2,
   UserName: "Lily",
   IsMale:   false,
  },
 }

 usersArr := arr.ValueOfSlice(users)
 usersArr = arr.WashColumnTag(usersArr, arr.WashTagJsonInitalLowerOpt())
 if bs, err := json.Marshal(usersArr); err == nil {
  fmt.Println(string(bs))
 } else {
  panic(err)
 }

}

输出

[
   {"uid":1,"userName":"Bobby","isMale":true},
   {"uid":2,"userName":"Lily","isMale":false}
]

 

参考:https://lingdor.medium.com/magicarray-use-to-data-translate-easily-8831b937bfc1

 

标签:arr,string,err,usersArr,json,MagicArray,Go,php,Id
From: https://www.cnblogs.com/a-xu/p/17929336.html

相关文章

  • [资源管理] SQL Server 通过Resouce Governor来限制用户资源的使用
    创建资源池CREATERESOURCEPOOL[rp_test]WITH(min_cpu_percent=0,max_cpu_percent=1,min_memory_percent=0,max_memory_percent=1,AFFINITYSCHEDULER=AUTO)GO创建负载组CREATEWORKLOADGROUP[wlp_test]WITH(group_max_requests=10,importance=High,request_m......
  • mongodb安装
    #!/bin/bash###############################################################FileName:install_redis.sh#Version:V1.0#Author:junwang#Organization:#CreatedTime:2021-04-1417:12:54#Description:###############################################......
  • mongo升级服务,数据迁移迁移
    3*升级到5.*,会碰到索引不兼容问题 usestars_salesvarcollections=db.getCollectionNames();collections.forEach(function(collection){varindexes=db[collection].getIndexes();print("Indexesforcollection"+collection+":");printjson(......
  • ArgoCD 添加私有仓库
    HTTPS方式访问私有仓库ArgoCDCLI添加私有仓库#argocdrepoaddhttps://github.com/argoproj/argocd-example-apps--username<username>--password<password>dashboard添加私有仓库SSHPrivateKeyCredentialArgoCDCLI添加known_hosts#ssh-keyscan-p35022192.168......
  • ArgoCD notifications 配置
    ArgoCDnotifications TriggersTriggers定义应发送通知的条件。定义包括名称、条件和通知模板参考。条件是一个谓词表达式,如果通知则返回true应该发送。Triggers列表1.on-created#Application创建2.on-deleted#Applic......
  • ArgoCD和Argo Rollouts自动化部署应用
    环境说明使用ArgoRollouts部署nginx应用:1.使用Rollout部署nginx2.采用canary部署策略3.结合Analysis进行渐进式交付4.结合IstioVirtualService进行自动化流量分割和流量迁移5.部署清单放在gitrepo中使用ArgoCD进行自动化部署:1.使用Application监视GitRepository2......
  • ArgoCD 添加私有仓库
    HTTPS方式访问私有仓库ArgoCDCLI添加私有仓库#argocdrepoaddhttps://github.com/argoproj/argocd-example-apps--username<username>--password<password>dashboard添加私有仓库SSHPrivateKeyCredentialArgoCDCLI添加known_hosts#ssh-keyscan-p35022192.168......
  • Argo Rollouts TrafficRouting结合Istio进行Canary流量管理基础
    ArgoRolloutsTrafficRouting概述流量治理技术实现如下:1.按百分比进行流量管理(即5%的流量应流向新版本,其余流量流向稳定版本)2.基于标头的路由(即将带有特定标头的请求发送到新版本)3.镜像流量,其中所有流量都被复制并并行发送到新版本(但响应被忽略)TrafficRouting配置apiVersi......
  • Argo Rollouts notifications
    ArgoRolloutsnotificationscm模板argo-rollouts-notification-configmap的configmap需要部署在argo-rollouts名称空间下。apiVersion:v1kind:ConfigMapmetadata:name:argo-rollouts-notification-configmapdata:template.analysis-run-error:|message:Rollo......
  • Argo Rollouts Canary结合Istio进行流量迁移实例
    环境说明用argorollouts金丝雀发布策略更新nginx服务。发布过程结合Isito和analysis。创建nsargo-demo#kubectlcreatensargo-demonamespace/argo-democreated启用Istio自动注入功能#kubectllabelnamespaceargo-demoistio-injection=enablednamespace/argo-demolabe......