ing
This commit is contained in:
		
							parent
							
								
									9b9325a126
								
							
						
					
					
						commit
						7df953742d
					
				
							
								
								
									
										52
									
								
								core/constants/network.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								core/constants/network.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,52 @@ | |||||||
|  | package constants | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"fmt" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type PortType int | ||||||
|  | 
 | ||||||
|  | const ( | ||||||
|  | 	PortTypeTCP PortType = iota | ||||||
|  | 	PortTypeUDP | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var ( | ||||||
|  | 	portTypeID = map[PortType]string{ | ||||||
|  | 		PortTypeTCP: "TCP", | ||||||
|  | 		PortTypeUDP: "UDP", | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	portTypeName = map[string]PortType{ | ||||||
|  | 		"TCP": PortTypeTCP, | ||||||
|  | 		"UDP": PortTypeUDP, | ||||||
|  | 	} | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func (pt PortType) String() string { | ||||||
|  | 	return portTypeID[pt] | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (pt PortType) MarshalJSON() ([]byte, error) { | ||||||
|  | 	value, ok := portTypeID[pt] | ||||||
|  | 	if !ok { | ||||||
|  | 		return nil, fmt.Errorf("Invalid EnumType[%s] value", pt) | ||||||
|  | 	} | ||||||
|  | 	return json.Marshal(value) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func (pt PortType) UnmarshalJSON(b []byte) error { | ||||||
|  | 	var s string | ||||||
|  | 	err := json.Unmarshal(b, &s) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	value, ok := portTypeName[s] | ||||||
|  | 	if !ok { | ||||||
|  | 		return fmt.Errorf("Invalid EnumType[%s] value", s) | ||||||
|  | 	} | ||||||
|  | 	pt = value | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user