mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	feature: add suport of mapstructure struct tags in struct-tag rule (#1241)
This commit is contained in:
		| @@ -98,16 +98,17 @@ func (w lintStructTagRule) Visit(node ast.Node) ast.Visitor { | ||||
| } | ||||
|  | ||||
| const ( | ||||
| 	keyASN1      = "asn1" | ||||
| 	keyBSON      = "bson" | ||||
| 	keyDatastore = "datastore" | ||||
| 	keyDefault   = "default" | ||||
| 	keyJSON      = "json" | ||||
| 	keyProtobuf  = "protobuf" | ||||
| 	keyRequired  = "required" | ||||
| 	keyURL       = "url" | ||||
| 	keyXML       = "xml" | ||||
| 	keyYAML      = "yaml" | ||||
| 	keyASN1         = "asn1" | ||||
| 	keyBSON         = "bson" | ||||
| 	keyDatastore    = "datastore" | ||||
| 	keyDefault      = "default" | ||||
| 	keyJSON         = "json" | ||||
| 	keyMapstructure = "mapstructure" | ||||
| 	keyProtobuf     = "protobuf" | ||||
| 	keyRequired     = "required" | ||||
| 	keyURL          = "url" | ||||
| 	keyXML          = "xml" | ||||
| 	keyYAML         = "yaml" | ||||
| ) | ||||
|  | ||||
| func (w lintStructTagRule) checkTagNameIfNeed(tag *structtag.Tag) (string, bool) { | ||||
| @@ -196,6 +197,11 @@ func (w lintStructTagRule) checkTaggedField(f *ast.Field) { | ||||
| 			if !ok { | ||||
| 				w.addFailure(f.Tag, msg) | ||||
| 			} | ||||
| 		case keyMapstructure: | ||||
| 			msg, ok := w.checkMapstructureTag(tag.Options) | ||||
| 			if !ok { | ||||
| 				w.addFailure(f.Tag, msg) | ||||
| 			} | ||||
| 		case keyProtobuf: | ||||
| 			msg, ok := w.checkProtobufTag(tag) | ||||
| 			if !ok { | ||||
| @@ -379,6 +385,21 @@ func (w lintStructTagRule) checkDatastoreTag(options []string) (string, bool) { | ||||
| 	return "", true | ||||
| } | ||||
|  | ||||
| func (w lintStructTagRule) checkMapstructureTag(options []string) (string, bool) { | ||||
| 	for _, opt := range options { | ||||
| 		switch opt { | ||||
| 		case "omitempty", "reminder", "squash": | ||||
| 		default: | ||||
| 			if w.isUserDefined(keyMapstructure, opt) { | ||||
| 				continue | ||||
| 			} | ||||
| 			return fmt.Sprintf("unknown option '%s' in Mapstructure tag", opt), false | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return "", true | ||||
| } | ||||
|  | ||||
| func (lintStructTagRule) typeValueMatch(t ast.Expr, val string) bool { | ||||
| 	tID, ok := t.(*ast.Ident) | ||||
| 	if !ok { | ||||
|   | ||||
| @@ -18,6 +18,7 @@ func TestStructTagWithUserOptions(t *testing.T) { | ||||
| 			"bson,gnu", | ||||
| 			"url,myURLOption", | ||||
| 			"datastore,myDatastoreOption", | ||||
| 			"mapstructure,myMapstructureOption", | ||||
| 		}, | ||||
| 	}) | ||||
| } | ||||
|   | ||||
							
								
								
									
										5
									
								
								testdata/struct_tag.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								testdata/struct_tag.go
									
									
									
									
										vendored
									
									
								
							| @@ -141,3 +141,8 @@ type Fields struct { | ||||
| 	Field      string `datastore:",noindex,flatten,omitempty"` | ||||
| 	OtherField string `datastore:",unknownOption"` // MATCH /unknown option 'unknownOption' in Datastore tag/ | ||||
| } | ||||
|  | ||||
| type MapStruct struct { | ||||
| 	Field1     string `mapstructure:",squash,reminder,omitempty"` | ||||
| 	OtherField string `mapstructure:",unknownOption"` // MATCH /unknown option 'unknownOption' in Mapstructure tag/ | ||||
| } | ||||
|   | ||||
							
								
								
									
										5
									
								
								testdata/struct_tag_user_options.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								testdata/struct_tag_user_options.go
									
									
									
									
										vendored
									
									
								
							| @@ -24,3 +24,8 @@ type Fields struct { | ||||
| 	Field      string `datastore:",noindex,flatten,omitempty,myDatastoreOption"` | ||||
| 	OtherField string `datastore:",unknownOption"` // MATCH /unknown option 'unknownOption' in Datastore tag/ | ||||
| } | ||||
|  | ||||
| type MapStruct struct { | ||||
| 	Field1     string `mapstructure:",squash,reminder,omitempty,myMapstructureOption"` | ||||
| 	OtherField string `mapstructure:",unknownOption"` // MATCH /unknown option 'unknownOption' in Mapstructure tag/ | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user