-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package converters | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"reflect" | ||
|
||
"github.com/lovego/struct_tag" | ||
"github.com/lovego/structs" | ||
) | ||
|
||
func ValidateHeader(typ reflect.Type) { | ||
if typ.Kind() != reflect.Struct { | ||
log.Panic("req.Header must be a struct.") | ||
} | ||
structs.Traverse(reflect.New(typ).Elem(), true, func(_ reflect.Value, f reflect.StructField) bool { | ||
if !isSupportedType(f.Type) { | ||
log.Panicf("req.Header.%s: type must be string, number or bool.", f.Name) | ||
} | ||
return false | ||
}) | ||
return | ||
} | ||
|
||
func ConvertHeader(value reflect.Value, map2strs map[string][]string) (err error) { | ||
structs.Traverse(value, true, func(v reflect.Value, f reflect.StructField) bool { | ||
key, _ := struct_tag.Lookup(string(f.Tag), "header") | ||
if key == "" { | ||
key = f.Name | ||
} | ||
values := map2strs[key] | ||
if len(values) > 0 && values[0] != "" { | ||
err = Set(v, values[0]) | ||
} | ||
return err != nil // if err != nil, stop Traverse | ||
}) | ||
return | ||
} | ||
|
||
func ValidateRespHeader(typ reflect.Type) { | ||
if typ.Kind() != reflect.Struct { | ||
log.Panic("resp.Header must be a struct.") | ||
} | ||
structs.Traverse(reflect.New(typ).Elem(), false, func(_ reflect.Value, f reflect.StructField) bool { | ||
if f.Type.Kind() != reflect.String { | ||
log.Panicf("req.Header.%s: type must be string.", f.Name) | ||
} | ||
return false | ||
}) | ||
return | ||
} | ||
|
||
func WriteRespHeader(value reflect.Value, header http.Header) { | ||
structs.Traverse(value, false, func(v reflect.Value, f reflect.StructField) bool { | ||
if value := v.String(); value != "" { | ||
key, _ := struct_tag.Lookup(string(f.Tag), "header") | ||
if key == "" { | ||
key = f.Name | ||
} | ||
header.Set(key, value) | ||
} | ||
return false | ||
}) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package converters | ||
|
||
import ( | ||
"log" | ||
"reflect" | ||
|
||
"github.com/lovego/structs" | ||
) | ||
|
||
func ValidateQuery(typ reflect.Type) { | ||
if typ.Kind() != reflect.Struct { | ||
log.Panic("req.Query must be a struct.") | ||
} | ||
structs.Traverse(reflect.New(typ).Elem(), true, func(_ reflect.Value, f reflect.StructField) bool { | ||
if !isSupportedType(f.Type) { | ||
log.Panicf("req.Query.%s: type must be string, number or bool.", f.Name) | ||
} | ||
return false | ||
}) | ||
return | ||
} | ||
|
||
func ConvertQuery(value reflect.Value, map2strs map[string][]string) (err error) { | ||
structs.Traverse(value, true, func(v reflect.Value, f reflect.StructField) bool { | ||
values := map2strs[lowercaseFirstLetter(f.Name)] | ||
if len(values) > 0 && values[0] != "" { | ||
err = Set(v, values[0]) | ||
} | ||
return err != nil // if err != nil, stop Traverse | ||
}) | ||
return | ||
} |
This file was deleted.
Oops, something went wrong.