| 
									
										
										
										
											2017-06-20 23:09:53 +08:00
										 |  |  | package extra | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/json-iterator/go" | 
					
						
							| 
									
										
										
										
											2018-07-01 15:16:28 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-06-20 23:09:53 +08:00
										 |  |  | 	"unicode" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-09 14:17:40 +08:00
										 |  |  | // SetNamingStrategy rename struct fields uniformly | 
					
						
							| 
									
										
										
										
											2017-06-20 23:09:53 +08:00
										 |  |  | func SetNamingStrategy(translate func(string) string) { | 
					
						
							|  |  |  | 	jsoniter.RegisterExtension(&namingStrategyExtension{jsoniter.DummyExtension{}, translate}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type namingStrategyExtension struct { | 
					
						
							|  |  |  | 	jsoniter.DummyExtension | 
					
						
							|  |  |  | 	translate func(string) string | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) { | 
					
						
							|  |  |  | 	for _, binding := range structDescriptor.Fields { | 
					
						
							| 
									
										
										
										
											2018-07-01 15:16:28 +08:00
										 |  |  | 		tag, hastag := binding.Field.Tag().Lookup("json") | 
					
						
							|  |  |  | 		if hastag { | 
					
						
							|  |  |  | 			tagParts := strings.Split(tag, ",") | 
					
						
							|  |  |  | 			if tagParts[0] == "-" { | 
					
						
							|  |  |  | 				continue // hidden field | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if tagParts[0] != "" { | 
					
						
							|  |  |  | 				continue // field explicitly named | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-02-22 10:12:08 +08:00
										 |  |  | 		binding.ToNames = []string{extension.translate(binding.Field.Name())} | 
					
						
							|  |  |  | 		binding.FromNames = []string{extension.translate(binding.Field.Name())} | 
					
						
							| 
									
										
										
										
											2017-06-20 23:09:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-09 14:17:40 +08:00
										 |  |  | // LowerCaseWithUnderscores one strategy to SetNamingStrategy for. It will change HelloWorld to hello_world. | 
					
						
							| 
									
										
										
										
											2017-06-20 23:09:53 +08:00
										 |  |  | func LowerCaseWithUnderscores(name string) string { | 
					
						
							|  |  |  | 	newName := []rune{} | 
					
						
							|  |  |  | 	for i, c := range name { | 
					
						
							|  |  |  | 		if i == 0 { | 
					
						
							|  |  |  | 			newName = append(newName, unicode.ToLower(c)) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if unicode.IsUpper(c) { | 
					
						
							|  |  |  | 				newName = append(newName, '_') | 
					
						
							|  |  |  | 				newName = append(newName, unicode.ToLower(c)) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				newName = append(newName, c) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return string(newName) | 
					
						
							|  |  |  | } |