Best Ginkgo code snippet using internal.withType
gutil_dump.go
Source:gutil_dump.go
1// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.2//3// This Source Code Form is subject to the terms of the MIT License.4// If a copy of the MIT was not distributed with this file,5// You can obtain one at https://github.com/gogf/gf.6package gutil7import (8 "bytes"9 "fmt"10 "io"11 "reflect"12 "strings"13 "github.com/gogf/gf/v2/os/gstructs"14 "github.com/gogf/gf/v2/text/gstr"15)16// iString is used for type assert api for String().17type iString interface {18 String() string19}20// iError is used for type assert api for Error().21type iError interface {22 Error() string23}24// iMarshalJSON is the interface for custom Json marshaling.25type iMarshalJSON interface {26 MarshalJSON() ([]byte, error)27}28// DumpOption specifies the behavior of function Export.29type DumpOption struct {30 WithType bool // WithType specifies dumping content with type information.31}32// Dump prints variables `values` to stdout with more manually readable.33func Dump(values ...interface{}) {34 for _, value := range values {35 DumpWithOption(value, DumpOption{36 WithType: false,37 })38 }39}40// DumpWithType acts like Dump, but with type information.41// Also see Dump.42func DumpWithType(values ...interface{}) {43 for _, value := range values {44 DumpWithOption(value, DumpOption{45 WithType: true,46 })47 }48}49// DumpWithOption returns variables `values` as a string with more manually readable.50func DumpWithOption(value interface{}, option DumpOption) {51 buffer := bytes.NewBuffer(nil)52 DumpTo(buffer, value, DumpOption{53 WithType: option.WithType,54 })55 fmt.Println(buffer.String())56}57// DumpTo writes variables `values` as a string in to `writer` with more manually readable58func DumpTo(writer io.Writer, value interface{}, option DumpOption) {59 buffer := bytes.NewBuffer(nil)60 doDump(value, "", buffer, doDumpOption{61 WithType: option.WithType,62 })63 _, _ = writer.Write(buffer.Bytes())64}65type doDumpOption struct {66 WithType bool67}68func doDump(value interface{}, indent string, buffer *bytes.Buffer, option doDumpOption) {69 if value == nil {70 buffer.WriteString(`<nil>`)71 return72 }73 var (74 reflectValue = reflect.ValueOf(value)75 reflectKind = reflectValue.Kind()76 reflectTypeName = reflect.TypeOf(value).String()77 newIndent = indent + dumpIndent78 )79 reflectTypeName = strings.ReplaceAll(reflectTypeName, `[]uint8`, `[]byte`)80 if !option.WithType {81 reflectTypeName = ""82 }83 for reflectKind == reflect.Ptr {84 reflectValue = reflectValue.Elem()85 reflectKind = reflectValue.Kind()86 }87 var (88 exportInternalInput = doDumpInternalInput{89 Value: value,90 Indent: indent,91 NewIndent: newIndent,92 Buffer: buffer,93 Option: option,94 ReflectValue: reflectValue,95 ReflectTypeName: reflectTypeName,96 }97 )98 switch reflectKind {99 case reflect.Slice, reflect.Array:100 doDumpSlice(exportInternalInput)101 case reflect.Map:102 doDumpMap(exportInternalInput)103 case reflect.Struct:104 doDumpStruct(exportInternalInput)105 case reflect.String:106 doDumpString(exportInternalInput)107 case reflect.Bool:108 doDumpBool(exportInternalInput)109 case110 reflect.Int,111 reflect.Int8,112 reflect.Int16,113 reflect.Int32,114 reflect.Int64,115 reflect.Uint,116 reflect.Uint8,117 reflect.Uint16,118 reflect.Uint32,119 reflect.Uint64,120 reflect.Float32,121 reflect.Float64,122 reflect.Complex64,123 reflect.Complex128:124 doDumpNumber(exportInternalInput)125 case reflect.Chan:126 buffer.WriteString(fmt.Sprintf(`<%s>`, reflect.TypeOf(value).String()))127 case reflect.Func:128 if reflectValue.IsNil() || !reflectValue.IsValid() {129 buffer.WriteString(`<nil>`)130 } else {131 buffer.WriteString(fmt.Sprintf(`<%s>`, reflect.TypeOf(value).String()))132 }133 default:134 doDumpDefault(exportInternalInput)135 }136}137type doDumpInternalInput struct {138 Value interface{}139 Indent string140 NewIndent string141 Buffer *bytes.Buffer142 Option doDumpOption143 ReflectValue reflect.Value144 ReflectTypeName string145}146func doDumpSlice(in doDumpInternalInput) {147 if b, ok := in.Value.([]byte); ok {148 if !in.Option.WithType {149 in.Buffer.WriteString(fmt.Sprintf(`"%s"`, addSlashesForString(string(b))))150 } else {151 in.Buffer.WriteString(fmt.Sprintf(152 `%s(%d) "%s"`,153 in.ReflectTypeName,154 len(string(b)),155 string(b),156 ))157 }158 return159 }160 if in.ReflectValue.Len() == 0 {161 if !in.Option.WithType {162 in.Buffer.WriteString("[]")163 } else {164 in.Buffer.WriteString(fmt.Sprintf("%s(0) []", in.ReflectTypeName))165 }166 return167 }168 if !in.Option.WithType {169 in.Buffer.WriteString("[\n")170 } else {171 in.Buffer.WriteString(fmt.Sprintf("%s(%d) [\n", in.ReflectTypeName, in.ReflectValue.Len()))172 }173 for i := 0; i < in.ReflectValue.Len(); i++ {174 in.Buffer.WriteString(in.NewIndent)175 doDump(in.ReflectValue.Index(i).Interface(), in.NewIndent, in.Buffer, in.Option)176 in.Buffer.WriteString(",\n")177 }178 in.Buffer.WriteString(fmt.Sprintf("%s]", in.Indent))179}180func doDumpMap(in doDumpInternalInput) {181 var (182 mapKeys = in.ReflectValue.MapKeys()183 )184 if len(mapKeys) == 0 {185 if !in.Option.WithType {186 in.Buffer.WriteString("{}")187 } else {188 in.Buffer.WriteString(fmt.Sprintf("%s(0) {}", in.ReflectTypeName))189 }190 return191 }192 var (193 maxSpaceNum = 0194 tmpSpaceNum = 0195 mapKeyStr = ""196 )197 for _, key := range mapKeys {198 tmpSpaceNum = len(fmt.Sprintf(`%v`, key.Interface()))199 if tmpSpaceNum > maxSpaceNum {200 maxSpaceNum = tmpSpaceNum201 }202 }203 if !in.Option.WithType {204 in.Buffer.WriteString("{\n")205 } else {206 in.Buffer.WriteString(fmt.Sprintf("%s(%d) {\n", in.ReflectTypeName, len(mapKeys)))207 }208 for _, mapKey := range mapKeys {209 tmpSpaceNum = len(fmt.Sprintf(`%v`, mapKey.Interface()))210 if mapKey.Kind() == reflect.String {211 mapKeyStr = fmt.Sprintf(`"%v"`, mapKey.Interface())212 } else {213 mapKeyStr = fmt.Sprintf(`%v`, mapKey.Interface())214 }215 // Map key and indent string dump.216 if !in.Option.WithType {217 in.Buffer.WriteString(fmt.Sprintf(218 "%s%v:%s",219 in.NewIndent,220 mapKeyStr,221 strings.Repeat(" ", maxSpaceNum-tmpSpaceNum+1),222 ))223 } else {224 in.Buffer.WriteString(fmt.Sprintf(225 "%s%s(%v):%s",226 in.NewIndent,227 mapKey.Type().String(),228 mapKeyStr,229 strings.Repeat(" ", maxSpaceNum-tmpSpaceNum+1),230 ))231 }232 // Map value dump.233 doDump(in.ReflectValue.MapIndex(mapKey).Interface(), in.NewIndent, in.Buffer, in.Option)234 in.Buffer.WriteString(",\n")235 }236 in.Buffer.WriteString(fmt.Sprintf("%s}", in.Indent))237}238func doDumpStruct(in doDumpInternalInput) {239 structFields, _ := gstructs.Fields(gstructs.FieldsInput{240 Pointer: in.Value,241 RecursiveOption: gstructs.RecursiveOptionEmbedded,242 })243 if len(structFields) == 0 {244 var (245 structContentStr = ""246 attributeCountStr = "0"247 )248 if v, ok := in.Value.(iString); ok {249 structContentStr = v.String()250 } else if v, ok := in.Value.(iError); ok {251 structContentStr = v.Error()252 } else if v, ok := in.Value.(iMarshalJSON); ok {253 b, _ := v.MarshalJSON()254 structContentStr = string(b)255 }256 if structContentStr == "" {257 structContentStr = "{}"258 } else {259 structContentStr = fmt.Sprintf(`"%s"`, addSlashesForString(structContentStr))260 attributeCountStr = fmt.Sprintf(`%d`, len(structContentStr)-2)261 }262 if !in.Option.WithType {263 in.Buffer.WriteString(structContentStr)264 } else {265 in.Buffer.WriteString(fmt.Sprintf(266 "%s(%s) %s",267 in.ReflectTypeName,268 attributeCountStr,269 structContentStr,270 ))271 }272 return273 }274 var (275 maxSpaceNum = 0276 tmpSpaceNum = 0277 )278 for _, field := range structFields {279 tmpSpaceNum = len(field.Name())280 if tmpSpaceNum > maxSpaceNum {281 maxSpaceNum = tmpSpaceNum282 }283 }284 if !in.Option.WithType {285 in.Buffer.WriteString("{\n")286 } else {287 in.Buffer.WriteString(fmt.Sprintf("%s(%d) {\n", in.ReflectTypeName, len(structFields)))288 }289 for _, field := range structFields {290 tmpSpaceNum = len(fmt.Sprintf(`%v`, field.Name()))291 in.Buffer.WriteString(fmt.Sprintf(292 "%s%s:%s",293 in.NewIndent,294 field.Name(),295 strings.Repeat(" ", maxSpaceNum-tmpSpaceNum+1),296 ))297 doDump(field.Value.Interface(), in.NewIndent, in.Buffer, in.Option)298 in.Buffer.WriteString(",\n")299 }300 in.Buffer.WriteString(fmt.Sprintf("%s}", in.Indent))301}302func doDumpNumber(in doDumpInternalInput) {303 if v, ok := in.Value.(iString); ok {304 s := v.String()305 if !in.Option.WithType {306 in.Buffer.WriteString(fmt.Sprintf(`"%v"`, addSlashesForString(s)))307 } else {308 in.Buffer.WriteString(fmt.Sprintf(309 `%s(%d) "%v"`,310 in.ReflectTypeName,311 len(s),312 addSlashesForString(s),313 ))314 }315 } else {316 doDumpDefault(in)317 }318}319func doDumpString(in doDumpInternalInput) {320 s := in.ReflectValue.String()321 if !in.Option.WithType {322 in.Buffer.WriteString(fmt.Sprintf(`"%v"`, addSlashesForString(s)))323 } else {324 in.Buffer.WriteString(fmt.Sprintf(325 `%s(%d) "%v"`,326 in.ReflectTypeName,327 len(s),328 addSlashesForString(s),329 ))330 }331}332func doDumpBool(in doDumpInternalInput) {333 var s string334 if in.ReflectValue.Bool() {335 s = `true`336 } else {337 s = `false`338 }339 if in.Option.WithType {340 s = fmt.Sprintf(`bool(%s)`, s)341 }342 in.Buffer.WriteString(s)343}344func doDumpDefault(in doDumpInternalInput) {345 s := fmt.Sprintf("%v", in.Value)346 s = gstr.Trim(s, `<>`)347 if !in.Option.WithType {348 in.Buffer.WriteString(s)349 } else {350 in.Buffer.WriteString(fmt.Sprintf("%s(%s)", in.ReflectTypeName, s))351 }352}353func addSlashesForString(s string) string {354 return gstr.ReplaceByMap(s, map[string]string{355 `"`: `\"`,356 "\r": `\r`,357 "\t": `\t`,358 "\n": `\n`,359 })360}...
withType
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4}5import (6func main() {7 fmt.Println("Hello World!")8}
withType
Using AI Code Generation
1import (2type A struct {3}4func main() {5 a := A{name: "Hello"}6 fmt.Println(a.withType())7}8import (9type A struct {10}11func (a A) withType() string {12}13func main() {14 a := A{name: "Hello"}15 fmt.Println(a.withType())16}
withType
Using AI Code Generation
1import "internal/1"2func main() {3 _1.Test()4}5import (6func Test() {7 fmt.Println("Test")8 _2.Test()9}10import "fmt"11func Test() {12 fmt.Println("Test")13}14type Person struct {15}16p := structbuilder.New("Person", "Bob", 42)17import (18func main() {19 p := structbuilder.New("Person", "Bob", 42)20 fmt.Println(p)21}22type Person struct {23}24p := structbuilder.New("Person", "Bob", 42)25import (
withType
Using AI Code Generation
1import "fmt"2type MyStruct struct {3}4func (ms *MyStruct) withType() {5 fmt.Println("withType method of MyStruct")6}7func main() {8 ms := new(MyStruct)9 ms.withType()10}11import "fmt"12type MyStruct struct {13}14func (ms *MyStruct) withType() {15 fmt.Println("withType method of MyStruct")16}17func main() {18 ms := new(MyStruct)19 ms.withType()20}
withType
Using AI Code Generation
1import "internal/1"2func main() {3 internal.1.withType()4}5func withType() {6 println("Hello World")7}8func TestWithType(t *testing.T) {9 withType()10}11func BenchmarkWithType(b *testing.B) {12 for i := 0; i < b.N; i++ {13 withType()14 }15}16func ExampleWithType() {17 withType()18}19func withType() {20 println("Hello World")21}22func TestWithType(t *testing.T) {23 withType()24}25func BenchmarkWithType(b *testing.B) {26 for i := 0; i < b.N; i++ {27 withType()28 }29}30func ExampleWithType() {31 withType()32}33func withType() {34 println("Hello World")35}36func TestWithType(t *testing.T) {37 withType()38}39func BenchmarkWithType(b *testing.B) {40 for i := 0; i < b.N; i++ {41 withType()42 }43}44func ExampleWithType() {45 withType()46}47func withType() {48 println("Hello World")49}50func TestWithType(t *testing.T) {51 withType()52}
withType
Using AI Code Generation
1import "internal/1"2func main() {3 internal.WithType()4}5func WithType() {6}
withType
Using AI Code Generation
1import (2func main() {3 obj := internalClass{"Hello"}4 obj.withType()5}6import (7func main() {8 obj := internalClass{"Hello"}9 obj.withType()10}11import (12func main() {13 obj := internalClass{"Hello"}14 obj.withType()15}16import (17func main() {18 obj := internalClass{"Hello"}19 obj.withType()20}21import (22func main() {23 obj := internalClass{"Hello"}24 obj.withType()25}26import (27func main() {28 obj := internalClass{"Hello"}29 obj.withType()30}31import (32func main() {33 obj := internalClass{"Hello"}34 obj.withType()35}36import (37func main() {38 obj := internalClass{"Hello"}39 obj.withType()40}
withType
Using AI Code Generation
1import (2func main() {3 internal.WithType()4}5import "fmt"6type internal struct{}7func (i internal) withType() {8 fmt.Println("withType")9}10func WithType() {11 i.withType()12}13The main package is special in Go. It is the package that is used to create the executable file. The main package is also the only package that can be compiled without any imports
withType
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, playground")4 var i1 = internal.Internal1{}5 i1.WithType()6}7import (8func main() {9 fmt.Println("Hello, playground")10 var i1 = internal.Internal1{}11 i1.WithType()12}13import (14type Internal1 struct {15}16func (i *Internal1) WithType() {17 fmt.Println("internal1 - withType")18}19import (20type Internal2 struct {21}22func (i *Internal2) WithType() {23 fmt.Println("internal2 - withType")24}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!