diff --git a/micro/microfunctions.go b/micro/microfunctions.go index 27c00204..9d42a014 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -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 +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index 7a8e198e..de9716d1 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -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) + } +}