github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/astutils/rewritejsontag_test.go (about) 1 package astutils 2 3 import ( 4 "fmt" 5 "github.com/iancoleman/strcase" 6 "github.com/unionj-cloud/go-doudou/v2/toolkit/pathutils" 7 "testing" 8 ) 9 10 func ExampleRewriteTag() { 11 file := pathutils.Abs("testdata/rewritejsontag.go") 12 config := RewriteTagConfig{ 13 File: file, 14 Omitempty: true, 15 ConvertFunc: strcase.ToLowerCamel, 16 Form: false, 17 } 18 result, err := RewriteTag(config) 19 if err != nil { 20 panic(err) 21 } 22 fmt.Println(result) 23 // Output: 24 //package main 25 // 26 //type base struct { 27 // Index string `json:"index,omitempty"` 28 // Type string `json:"type,omitempty"` 29 //} 30 // 31 //type struct1 struct { 32 // base 33 // Name string `json:"name,omitempty"` 34 // StructType int `json:"structType,omitempty" dd:"awesomtag"` 35 // Format string `dd:"anothertag" json:"format,omitempty"` 36 // Pos int `json:"pos,omitempty"` 37 //} 38 } 39 40 func Test_isExport(t *testing.T) { 41 type args struct { 42 field string 43 } 44 tests := []struct { 45 name string 46 args args 47 want bool 48 }{ 49 { 50 name: "", 51 args: args{ 52 field: "unExportField", 53 }, 54 want: false, 55 }, 56 { 57 name: "", 58 args: args{ 59 field: "ExportField", 60 }, 61 want: true, 62 }, 63 } 64 for _, tt := range tests { 65 t.Run(tt.name, func(t *testing.T) { 66 if got := isExport(tt.args.field); got != tt.want { 67 t.Errorf("isExport() = %v, want %v", got, tt.want) 68 } 69 }) 70 } 71 } 72 73 func Test_extractJsonPropName(t *testing.T) { 74 type args struct { 75 tag string 76 } 77 tests := []struct { 78 name string 79 args args 80 want string 81 }{ 82 { 83 name: "", 84 args: args{ 85 tag: `json:"name, omitempty"`, 86 }, 87 want: "name", 88 }, 89 { 90 name: "", 91 args: args{ 92 tag: `json:"name"`, 93 }, 94 want: "name", 95 }, 96 { 97 name: "", 98 args: args{ 99 tag: `json:"-, omitempty"`, 100 }, 101 want: "-", 102 }, 103 { 104 name: "", 105 args: args{ 106 tag: `json:"-"`, 107 }, 108 want: "-", 109 }, 110 } 111 for _, tt := range tests { 112 t.Run(tt.name, func(t *testing.T) { 113 if got := extractJsonPropName(tt.args.tag); got != tt.want { 114 t.Errorf("extractJsonPropName() = %v, want %v", got, tt.want) 115 } 116 }) 117 } 118 } 119 120 func ExampleRewriteTagForm() { 121 file := pathutils.Abs("testdata/rewritejsontag.go") 122 config := RewriteTagConfig{ 123 File: file, 124 Omitempty: true, 125 ConvertFunc: strcase.ToLowerCamel, 126 Form: true, 127 } 128 result, err := RewriteTag(config) 129 if err != nil { 130 panic(err) 131 } 132 fmt.Println(result) 133 // Output: 134 //package main 135 // 136 //type base struct { 137 // Index string `json:"index,omitempty" form:"index,omitempty"` 138 // Type string `json:"type,omitempty" form:"type,omitempty"` 139 //} 140 // 141 //type struct1 struct { 142 // base 143 // Name string `json:"name,omitempty" form:"name,omitempty"` 144 // StructType int `json:"structType,omitempty" dd:"awesomtag" form:"structType,omitempty"` 145 // Format string `dd:"anothertag" json:"format,omitempty" form:"format,omitempty"` 146 // Pos int `json:"pos,omitempty" form:"pos,omitempty"` 147 //} 148 }