github.com/hedzr/evendeep@v0.4.8/tests/complex_test.go (about) 1 //go:build ignore 2 // +build ignore 3 4 package tests_test 5 6 import ( 7 "reflect" 8 "testing" 9 10 "gopkg.in/yaml.v3" 11 12 "github.com/hedzr/evendeep" 13 "github.com/hedzr/evendeep/flags/cms" 14 ) 15 16 func TestComplexStructure(t *testing.T) { 17 type A struct { 18 A bool 19 } 20 var from = A{} 21 var to = A{true} 22 var c = evendeep.New( 23 evendeep.WithCopyStrategyOpt, 24 evendeep.WithStrategies(cms.OmitIfEmpty), 25 ) 26 var err = c.CopyTo(from, &to) 27 if err != nil { 28 t.Fatalf("err: %v", err) 29 } 30 if to.A == false { 31 t.Fatalf("expecting %v", true) 32 } 33 } 34 35 func TestComplexStructure9(t *testing.T) { 36 const src = `--- 37 app: 38 bgo: 39 build: 40 for: 41 - "linux/amd64" 42 - "windows/amd64" 43 - "darwin/amd64" 44 - "darwin/arm64" 45 os: [ linux ] 46 arch: [ amd64, arm64 ] 47 reduce: true 48 upx: 49 enable: false 50 params: [ "-9" ] 51 52 projects: 53 000-default-group: 54 leading-text: 55 items: 56 003-generic: 57 dir: examples/generic 58 os: [ "darwin","linux" ] 59 arch: [ "amd64" ] 60 upx: 61 enable: false 62 params: [ "-min" ] 63 004: 64 dir: examples/demo 65 upx: 66 enable: true 67 params: [ "-best", "-clean" ] 68 69 ` 70 // var b []byte 71 var err error 72 73 var holder Doc 74 err = yaml.Unmarshal([]byte(src), &holder) 75 t.Logf("holder: %+v", holder) 76 77 var projectGroup = holder.Projects["000-default-group"] 78 var p1 = projectGroup.Items["003-generic"] 79 var p2 = projectGroup.Items["004"] 80 var c = evendeep.New( 81 // evendeep.WithStrategies(cms.OmitIfZero), 82 evendeep.WithCopyStrategyOpt, 83 ) 84 85 t.Run("copy upx", func(t *testing.T) { 86 var upx Upx 87 if err = c.CopyTo(holder.Upx, &upx); err != nil { 88 t.Fatal(err) 89 } 90 if err = c.CopyTo(projectGroup.Upx, &upx); err != nil { 91 t.Fatal(err) 92 } else { 93 if upx.Enable != true { 94 t.Fatal("expecting true") 95 } 96 if !evendeep.DeepEqual(upx.Params, []string{"-best", "-clean"}) { 97 t.Fatalf("expecting %v but got %v", []string{"-best", "-clean"}, upx.Params) 98 } 99 } 100 }) 101 102 t.Run("copy os-arch", func(t *testing.T) { 103 var common Common 104 if err = c.CopyTo(holder.Common, &common); err != nil { 105 t.Fatal(err) 106 } 107 if err = c.CopyTo(p1.Common, &common); err != nil { 108 t.Fatal(err) 109 } else { 110 if !evendeep.DeepEqual(common.Os, []string{"darwin", "linux"}) { 111 t.Fatalf("expecting %v but got %v", []string{"darwin", "linux"}, common.Os) 112 } 113 if !evendeep.DeepEqual(common.Arch, []string{"amd64"}) { 114 t.Fatalf("expecting %v but got %v", []string{"amd64"}, common.Arch) 115 } 116 } 117 118 if err = c.CopyTo(holder.Common, &common); err != nil { 119 t.Fatal(err) 120 } 121 if err = c.CopyTo(p2.Common, &common); err != nil { 122 t.Fatal(err) 123 } else { 124 if !evendeep.DeepEqual(common.Os, []string{"linux"}) { 125 t.Fatalf("expecting %v but got %v", []string{"linux"}, common.Os) 126 } 127 if !evendeep.DeepEqual(common.Arch, []string{"amd64", "arm64"}) { 128 t.Fatalf("expecting %v but got %v", []string{"amd64", "arm64"}, common.Arch) 129 } 130 } 131 }) 132 } 133 134 func Rindirect(reflectValue reflect.Value) reflect.Value { 135 for reflectValue.Kind() == reflect.Ptr { 136 reflectValue = reflectValue.Elem() 137 } 138 return reflectValue 139 } 140 141 func TestChan01(t *testing.T) { 142 t.Run(`check cap`, func(t *testing.T) { 143 H := make(chan int, 5) 144 v := reflect.ValueOf(H) 145 t.Logf(`H.cap: %v`, v.Cap()) 146 147 H = make(chan int) 148 v = reflect.ValueOf(H) 149 t.Logf(`H.cap: %v`, v.Cap()) 150 }) 151 152 t.Run(`set field`, func(t *testing.T) { 153 type A struct { 154 H chan int 155 } 156 var a A 157 v := reflect.ValueOf(&a) 158 vind := Rindirect(v) 159 fv := vind.FieldByName(`H`) 160 cv := reflect.MakeChan(fv.Type(), 5) 161 fv.Set(cv) 162 t.Logf(`a: %v`, a) 163 }) 164 } 165 166 func TestComplexStructure901(t *testing.T) { 167 const src = `--- 168 app: 169 bgo: 170 build: 171 for: 172 - "linux/amd64" 173 - "windows/amd64" 174 - "darwin/amd64" 175 - "darwin/arm64" 176 os: [ linux ] 177 arch: [ amd64, arm64 ] 178 reduce: true 179 upx: 180 enable: false 181 params: [ "-9" ] 182 183 projects: # Projects map[string]ProjectGroup 184 000-default-group: 185 leading-text: dummy-title 186 items: # Items map[string]*ProjectWrap 187 003-generic: 188 dir: examples/generic 189 os: [ "darwin","linux" ] 190 arch: [ "amd64" ] 191 upx: 192 enable: false 193 params: [ "-min" ] 194 # 004: 195 # dir: examples/demo 196 upx: 197 enable: true 198 params: [ "-best", "-clean" ] 199 200 ` 201 // var b []byte 202 var err error 203 204 var holder Doc 205 err = yaml.Unmarshal([]byte(src), &holder) 206 t.Logf("holder: %+v", holder) 207 208 var c = evendeep.New( 209 // evendeep.WithStrategies(cms.OmitIfZero), 210 // evendeep.WithCopyStrategyOpt, 211 ) 212 213 // t.Run("copy project-group map", func(t *testing.T) { 214 // var conf Doc 215 // // var projectGroup = holder.Projects["000-default-group"] 216 // if err = c.CopyTo(holder.Projects, &conf.Projects); err != nil { 217 // t.Fatal(err) 218 // } 219 // if !evendeep.DeepEqual(holder, &conf) { 220 // t.Fatalf("expecting \n\n%+v\n\nbut got\n\n%+v", holder, conf) 221 // } 222 // }) 223 224 t.Run("copy all Doc", func(t *testing.T) { 225 var conf Doc 226 if err = c.CopyTo(holder, &conf); err != nil { 227 t.Fatalf("%+v", err) 228 } 229 if diff, equal := evendeep.DeepDiff(holder, conf); !equal { 230 t.Fatalf("expecting \n\n%+v\n\nbut got\n\n%+v\n\ndiff is:\n\n%+v", holder, conf, diff) 231 } 232 }) 233 } 234 235 // ----------------------------------------------------- 236 237 type Doc struct { 238 App `yaml:"app,omitempty"` 239 } 240 241 type App struct { 242 Build `yaml:"bgo,omitempty"` 243 } 244 245 type Build struct { 246 BgoSettings `yaml:"build,omitempty"` 247 } 248 249 // ----------------------------------------------------- 250 251 type ( 252 //nolint:lll //no 253 CommonBase struct { 254 OS string `yaml:"-" json:"-" toml:"-"` // just for string template expansion 255 ARCH string `yaml:"-" json:"-" toml:"-"` // just for string template expansion 256 257 Args []string `yaml:"args,omitempty,flow" json:"args,omitempty" toml:"args,omitempty"` 258 Ldflags []string `yaml:"ldflags,omitempty,flow" json:"ldflags,omitempty" toml:"ldflags,omitempty"` // default ldflags is to get the smaller build for releasing 259 Asmflags []string `yaml:"asmflags,omitempty,flow" json:"asmflags,omitempty" toml:"asmflags,omitempty"` // 260 Gcflags []string `yaml:"gcflags,omitempty,flow" json:"gcflags,omitempty" toml:"gcflags,omitempty"` // 261 Gccgoflags []string `yaml:"gccgoflags,omitempty,flow" json:"gccgoflags,omitempty" toml:"gccgoflags,omitempty"` // 262 Tags []string `yaml:"tags,omitempty,flow" json:"tags,omitempty" toml:"tags,omitempty"` // 263 // Cgo option 264 Cgo bool `yaml:",omitempty" json:"cgo,omitempty" toml:"cgo,omitempty"` // 265 // Race option enables data race detection. 266 // Supported only on linux/amd64, freebsd/amd64, darwin/amd64, windows/amd64, 267 // linux/ppc64le and linux/arm64 (only for 48-bit VMA). 268 Race bool `yaml:",omitempty" json:"race,omitempty" toml:"race,omitempty"` // 269 // Msan option enables interoperation with memory sanitizer. 270 // Supported only on linux/amd64, linux/arm64 271 // and only with Clang/LLVM as the host C compiler. 272 // On linux/arm64, pie build mode will be used. 273 Msan bool `yaml:",omitempty" json:"msan,omitempty" toml:"msan,omitempty"` // 274 // Asan enables interoperation with address sanitizer. 275 // Supported only on linux/arm64, linux/amd64. 276 // Supported only on linux/amd64 or linux/arm64 and only with GCC 7 and higher 277 // or Clang/LLVM 9 and higher. 278 Asan bool `yaml:",omitempty" json:"asan,omitempty" toml:"asan,omitempty"` // 279 // 280 Mod string `yaml:",omitempty" json:"mod,omitempty" toml:"mod,omitempty"` // -mod reaonly,vendor,mod 281 Amd64 string `yaml:",omitempty" json:"goamd64,omitempty" toml:"goamd64,omitempty"` // GOAMD64 v1,v2,v3,v4 282 Gocmd string `yaml:",omitempty" json:"gocmd,omitempty" toml:"gocmd,omitempty"` // -gocmd go 283 Gen bool `yaml:",omitempty" json:"gen,omitempty" toml:"gen,omitempty"` // go generate at first? 284 Install bool `yaml:",omitempty" json:"install,omitempty" toml:"install,omitempty"` // install binary to $GOPATH/bin like 'go install' ? 285 Debug bool `yaml:",omitempty" json:"debug,omitempty" toml:"debug,omitempty"` // true to produce a larger build with debug info 286 DisableResult bool `yaml:"disable-result,omitempty" json:"disable_result,omitempty" toml:"disable_result,omitempty"` // no ll (Shell list) building result 287 288 // -X for -ldflags, 289 // -X importpath.name=value 290 // Set the value of the string variable in importpath named name to value. 291 // Note that before Go 1.5 this option took two separate arguments. 292 // Now it takes one argument split on the first = sign. 293 Extends []PackageNameValues `yaml:"extends,omitempty" json:"extends,omitempty" toml:"extends,omitempty"` // 294 CmdrSpecials bool `yaml:"cmdr,omitempty" json:"cmdr,omitempty" toml:"cmdr,omitempty"` 295 CmdrVersion string `yaml:"cmdr-version,omitempty" json:"cmdr-version,omitempty" toml:"cmdr-version,omitempty"` 296 } 297 298 // Upx params package. 299 Upx struct { 300 Enable bool `yaml:",omitempty" json:"enable,omitempty" toml:"enable,omitempty"` 301 Params []string `yaml:",omitempty" json:"params,omitempty" toml:"params,omitempty"` 302 } 303 304 PackageNameValues struct { 305 Package string `yaml:"pkg,omitempty" json:"package,omitempty" toml:"package,omitempty"` 306 Values map[string]string `yaml:"values,omitempty" json:"values,omitempty" toml:"values,omitempty"` 307 } 308 309 //nolint:lll //no 310 Common struct { 311 CommonBase `yaml:",omitempty,inline,flow" json:",omitempty" toml:""` 312 313 Disabled bool `yaml:"disabled,omitempty" json:"disabled,omitempty" toml:"disabled,omitempty"` 314 KeepWorkdir bool `yaml:"keep-workdir,omitempty" json:"keep_workdir,omitempty" toml:"keep_workdir,omitempty"` 315 UseWorkDir string `yaml:"use-workdir,omitempty" json:"use_work_dir,omitempty" toml:"use_work_dir,omitempty"` 316 For []string `yaml:"for,omitempty,flow" json:"for,omitempty" toml:"for,omitempty"` 317 Os []string `yaml:"os,omitempty,flow" json:"os,omitempty" toml:"os,omitempty"` 318 Arch []string `yaml:"arch,omitempty,flow" json:"arch,omitempty" toml:"arch,omitempty"` 319 Goroot string `yaml:"goroot,omitempty,flow" json:"goroot,omitempty" toml:"goroot,omitempty"` 320 PreAction string `yaml:"pre-action,omitempty" json:"pre_action,omitempty" toml:"pre_action,omitempty"` // bash script 321 PostAction string `yaml:"post-action,omitempty" json:"post_action,omitempty" toml:"post_action,omitempty"` // bash script 322 PreActionFile string `yaml:"pre-action-file,omitempty" json:"pre_action_file,omitempty" toml:"pre_action_file,omitempty"` // bash script 323 PostActionFile string `yaml:"post-action-file,omitempty" json:"post_action_file,omitempty" toml:"post_action_file,omitempty"` // bash script 324 325 DeepReduce bool `yaml:"reduce,omitempty" json:"reduce,omitempty" toml:"reduce,omitempty"` 326 Upx Upx `yaml:",omitempty" json:"upx,omitempty" toml:"upx,omitempty"` 327 } 328 ) 329 330 type ( 331 DynBuildInfo struct { 332 ProjectName string 333 AppName string 334 Version string // copied from Project.Version 335 BgoGroupKey string // project-group key in .bgo.yml 336 BgoGroupLeadingText string // same above, 337 HasGoMod bool // 338 GoModFile string // 339 GOROOT string // force using a special GOROOT 340 Dir string 341 } 342 343 Info struct { 344 GoVersion string // the result from 'go version' 345 GoVersionMajor int // 1 346 GoVersionMinor int // 17 347 GitVersion string // the result from `git describe --tags --abbrev=0` 348 GitRevision string // revision, git hash code, from `git rev-parse --short HEAD` 349 GitSummary string // `git describe --tags --dirty --always` 350 GitDesc string // `git log --online -1` 351 BuilderComment string // 352 BuildTime string // format is like '2023-01-22T09:26:07+08:00' 353 GOOS string // a copy from runtime.GOOS 354 GOARCH string // a copy from runtime.GOARCH 355 GOVERSION string // a copy from runtime.Version() 356 357 RandomString string 358 RandomInt int 359 Serial int64 360 } 361 ) 362 363 type TargetPlatforms struct { 364 // OsArchMap is a map by key OS and CPUArch 365 // key1: os 366 // key2: arch 367 OsArchMap map[string]map[string]bool `yaml:"os-arch-map"` 368 // Sources is a map by key PackageName 369 // key: packageName 370 Sources map[string]*DynBuildInfo `yaml:"sources"` 371 } 372 373 type ( 374 ProjectWrap struct { 375 Project `yaml:",omitempty,inline,flow" json:"project" toml:"project,omitempty"` 376 } 377 //nolint:lll //no 378 Project struct { 379 Name string `yaml:"name,omitempty" json:"name,omitempty" toml:"name,omitempty"` // appName if specified 380 Dir string `yaml:"dir" json:"dir" toml:"dir"` // root dir of this module/cli-app 381 Package string `yaml:"package,omitempty" json:"package,omitempty" toml:"package,omitempty"` // pkgName or mainGo 382 MainGo string `yaml:"main-go,omitempty" json:"main_go,omitempty" toml:"main_go,omitempty"` // default: 'main.go', // or: "./cli", "./cli/main.go", ... 383 Version string `yaml:"version,omitempty" json:"version,omitempty" toml:"version,omitempty"` // 384 385 // In current and earlier releases, MainGo is reserved and never used. 386 387 *Common `yaml:",omitempty,inline,flow" json:",omitempty" toml:",omitempty"` 388 389 // Cmdr building opts 390 391 // More Posts: 392 // packaging methods 393 // coverage 394 // uploading 395 // docker 396 // github release 397 // Before 398 // fmt, lint, test, cyclo, ... 399 400 keyName string 401 tp *TargetPlatforms `copy:"-"` 402 } 403 ) 404 405 type ( 406 // BgoSettings for go build. 407 // Generally it's loaded from '.bgo.yml'. 408 BgoSettings struct { 409 *Common `yaml:",omitempty,inline" json:",omitempty" toml:",omitempty"` 410 411 Scope string `yaml:"scope,omitempty" json:"scope,omitempty" toml:"scope,omitempty"` 412 Projects map[string]ProjectGroup `yaml:"projects" json:"projects" toml:"projects"` 413 414 Output Output `yaml:",omitempty" json:"output" toml:"output,omitempty"` 415 Excludes []string `yaml:",omitempty,flow" json:"excludes,omitempty" toml:"excludes,omitempty"` 416 Goproxy string `yaml:",omitempty" json:"goproxy,omitempty" toml:"goproxy,omitempty"` 417 418 SavedAs []string `yaml:"saved-as,omitempty" json:"saved_as,omitempty" toml:"saved_as,omitempty"` 419 } 420 421 //nolint:lll //no 422 Output struct { 423 Dir string `yaml:",omitempty" json:"dir,omitempty" toml:"dir,omitempty"` // Base Dir 424 SplitTo string `yaml:"split-to,omitempty" json:"split_to,omitempty" toml:"split_to,omitempty"` // see build.Context, Group, Project, ... | see also BuildContext struct 425 NamedAs string `yaml:"named-as,omitempty" json:"named_as,omitempty" toml:"named_as,omitempty"` // see build.Context, 426 SuffixAs string `yaml:"suffix-as,omitempty" json:"-" toml:"-"` // NEVER USED. 427 ZipSuffixAs string `yaml:"zip-suffix-as,omitempty" json:"-" toml:"-"` // NEVER USED. supported: gz/tgz, bz2, xz/txz, 7z 428 } 429 430 ProjectGroup struct { 431 LeadingText string `yaml:"leading-text,omitempty" json:"leading_text,omitempty" toml:"leading_text,omitempty"` //nolint:lll //no 432 Items map[string]*ProjectWrap `yaml:"items" json:"items,omitempty" toml:"items,omitempty"` 433 *Common `yaml:",omitempty,inline" json:",omitempty" toml:",omitempty"` 434 } 435 )