|
| 1 | +package codegen |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/assert" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestProperty_GoTypeDef(t *testing.T) { |
| 9 | + type fields struct { |
| 10 | + GlobalStateDisableRequiredReadOnlyAsPointer bool |
| 11 | + Schema Schema |
| 12 | + Required bool |
| 13 | + Nullable bool |
| 14 | + ReadOnly bool |
| 15 | + WriteOnly bool |
| 16 | + } |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + fields fields |
| 20 | + want string |
| 21 | + }{ |
| 22 | + { |
| 23 | + // When pointer is skipped by setting flag SkipOptionalPointer, the |
| 24 | + // flag will never be pointer irrespective of other flags. |
| 25 | + name: "Set skip optional pointer type for go type", |
| 26 | + fields: fields{ |
| 27 | + Schema: Schema{ |
| 28 | + SkipOptionalPointer: true, |
| 29 | + RefType: "", |
| 30 | + GoType: "int", |
| 31 | + }, |
| 32 | + }, |
| 33 | + want: "int", |
| 34 | + }, |
| 35 | + |
| 36 | + { |
| 37 | + // if the field is optional, it will always be pointer irrespective of other |
| 38 | + // flags, given that pointer type is not skipped by setting SkipOptionalPointer |
| 39 | + // flag to true |
| 40 | + name: "When the field is optional", |
| 41 | + fields: fields{ |
| 42 | + Schema: Schema{ |
| 43 | + SkipOptionalPointer: false, |
| 44 | + RefType: "", |
| 45 | + GoType: "int", |
| 46 | + }, |
| 47 | + Required: false, |
| 48 | + }, |
| 49 | + want: "*int", |
| 50 | + }, |
| 51 | + |
| 52 | + { |
| 53 | + // if the field(custom-type) is optional, it will NOT be a pointer if |
| 54 | + // SkipOptionalPointer flag is set to true |
| 55 | + name: "Set skip optional pointer type for ref type", |
| 56 | + fields: fields{ |
| 57 | + Schema: Schema{ |
| 58 | + SkipOptionalPointer: true, |
| 59 | + RefType: "CustomType", |
| 60 | + GoType: "int", |
| 61 | + }, |
| 62 | + Required: false, |
| 63 | + }, |
| 64 | + want: "CustomType", |
| 65 | + }, |
| 66 | + |
| 67 | + // For the following test cases, SkipOptionalPointer flag is false. |
| 68 | + { |
| 69 | + name: "When field is required and not nullable", |
| 70 | + fields: fields{ |
| 71 | + Schema: Schema{ |
| 72 | + SkipOptionalPointer: false, |
| 73 | + GoType: "int", |
| 74 | + }, |
| 75 | + Required: true, |
| 76 | + Nullable: false, |
| 77 | + }, |
| 78 | + want: "int", |
| 79 | + }, |
| 80 | + |
| 81 | + { |
| 82 | + name: "When field is required and nullable", |
| 83 | + fields: fields{ |
| 84 | + Schema: Schema{ |
| 85 | + SkipOptionalPointer: false, |
| 86 | + GoType: "int", |
| 87 | + }, |
| 88 | + Required: true, |
| 89 | + Nullable: true, |
| 90 | + }, |
| 91 | + want: "*int", |
| 92 | + }, |
| 93 | + |
| 94 | + { |
| 95 | + name: "When field is optional and not nullable", |
| 96 | + fields: fields{ |
| 97 | + Schema: Schema{ |
| 98 | + SkipOptionalPointer: false, |
| 99 | + GoType: "int", |
| 100 | + }, |
| 101 | + Required: false, |
| 102 | + Nullable: false, |
| 103 | + }, |
| 104 | + want: "*int", |
| 105 | + }, |
| 106 | + |
| 107 | + { |
| 108 | + name: "When field is optional and nullable", |
| 109 | + fields: fields{ |
| 110 | + Schema: Schema{ |
| 111 | + SkipOptionalPointer: false, |
| 112 | + GoType: "int", |
| 113 | + }, |
| 114 | + Required: false, |
| 115 | + Nullable: true, |
| 116 | + }, |
| 117 | + want: "*int", |
| 118 | + }, |
| 119 | + |
| 120 | + // Following tests cases for non-nullable and required; and skip pointer is not opted |
| 121 | + { |
| 122 | + name: "When field is readOnly it will always be pointer", |
| 123 | + fields: fields{ |
| 124 | + Schema: Schema{ |
| 125 | + SkipOptionalPointer: false, |
| 126 | + GoType: "int", |
| 127 | + }, |
| 128 | + ReadOnly: true, |
| 129 | + Required: true, |
| 130 | + }, |
| 131 | + want: "*int", |
| 132 | + }, |
| 133 | + |
| 134 | + { |
| 135 | + name: "When field is readOnly and read only pointer disabled", |
| 136 | + fields: fields{ |
| 137 | + GlobalStateDisableRequiredReadOnlyAsPointer: true, |
| 138 | + Schema: Schema{ |
| 139 | + SkipOptionalPointer: false, |
| 140 | + GoType: "int", |
| 141 | + }, |
| 142 | + ReadOnly: true, |
| 143 | + Required: true, |
| 144 | + }, |
| 145 | + want: "int", |
| 146 | + }, |
| 147 | + |
| 148 | + { |
| 149 | + name: "When field is readOnly and optional", |
| 150 | + fields: fields{ |
| 151 | + Schema: Schema{ |
| 152 | + SkipOptionalPointer: false, |
| 153 | + GoType: "int", |
| 154 | + }, |
| 155 | + ReadOnly: true, |
| 156 | + Required: false, |
| 157 | + }, |
| 158 | + want: "*int", |
| 159 | + }, |
| 160 | + { |
| 161 | + name: "When field is readOnly and optional and read only pointer disabled", |
| 162 | + fields: fields{ |
| 163 | + GlobalStateDisableRequiredReadOnlyAsPointer: true, |
| 164 | + Schema: Schema{ |
| 165 | + SkipOptionalPointer: false, |
| 166 | + GoType: "int", |
| 167 | + }, |
| 168 | + ReadOnly: true, |
| 169 | + Required: false, |
| 170 | + }, |
| 171 | + want: "*int", |
| 172 | + }, |
| 173 | + |
| 174 | + // When field is write only, it will always be pointer unless pointer is |
| 175 | + // skipped by setting SkipOptionalPointer flag |
| 176 | + { |
| 177 | + name: "When field is write only and read only pointer disabled", |
| 178 | + fields: fields{ |
| 179 | + GlobalStateDisableRequiredReadOnlyAsPointer: true, |
| 180 | + Schema: Schema{ |
| 181 | + SkipOptionalPointer: false, |
| 182 | + GoType: "int", |
| 183 | + }, |
| 184 | + WriteOnly: true, |
| 185 | + }, |
| 186 | + want: "*int", |
| 187 | + }, |
| 188 | + |
| 189 | + { |
| 190 | + name: "When field is write only and read only pointer enabled", |
| 191 | + fields: fields{ |
| 192 | + GlobalStateDisableRequiredReadOnlyAsPointer: false, |
| 193 | + Schema: Schema{ |
| 194 | + SkipOptionalPointer: false, |
| 195 | + GoType: "int", |
| 196 | + }, |
| 197 | + WriteOnly: true, |
| 198 | + }, |
| 199 | + want: "*int", |
| 200 | + }, |
| 201 | + } |
| 202 | + for _, tt := range tests { |
| 203 | + t.Run(tt.name, func(t *testing.T) { |
| 204 | + globalState.options.Compatibility.DisableRequiredReadOnlyAsPointer = tt.fields.GlobalStateDisableRequiredReadOnlyAsPointer |
| 205 | + p := Property{ |
| 206 | + Schema: tt.fields.Schema, |
| 207 | + Required: tt.fields.Required, |
| 208 | + Nullable: tt.fields.Nullable, |
| 209 | + ReadOnly: tt.fields.ReadOnly, |
| 210 | + WriteOnly: tt.fields.WriteOnly, |
| 211 | + } |
| 212 | + assert.Equal(t, tt.want, p.GoTypeDef()) |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
0 commit comments