mirror of
https://github.com/ManyakRus/starter.git
synced 2025-11-24 22:53:52 +02:00
сделал Find_Tag_JSON()
This commit is contained in:
@@ -1868,3 +1868,19 @@ func Round_Float64_WithPrecision(x float64, precision int) float64 {
|
||||
Otvet := math.Round(x*pow) / pow
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Find_Tag_JSON - возвращает тег json для полей структуры
|
||||
func Find_Tag_JSON(Struct1 any, FieldName string) (string, error) {
|
||||
var Otvet string
|
||||
var err error
|
||||
|
||||
field, ok := reflect.TypeOf(Struct1).Elem().FieldByName(FieldName)
|
||||
if !ok {
|
||||
err = fmt.Errorf("Field %s not found in type %T", FieldName, Struct1)
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
Otvet = field.Tag.Get("json")
|
||||
|
||||
return Otvet, err
|
||||
}
|
||||
|
||||
@@ -1573,3 +1573,17 @@ func TestRound_Float64_WithPrecision2(t *testing.T) {
|
||||
t.Errorf("Expected 1.1, but got %f", Otvet)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFind_Tag_JSON(t *testing.T) {
|
||||
type StructTest struct {
|
||||
Test string `json:"test"`
|
||||
}
|
||||
StructTest1 := &StructTest{}
|
||||
Otvet, err := Find_Tag_JSON(StructTest1, "Test")
|
||||
if err != nil {
|
||||
t.Errorf("Error: Expected nil, but got %v", err)
|
||||
}
|
||||
if Otvet != "test" {
|
||||
t.Errorf("Expected 'test', but got %s", Otvet)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user