diff --git a/bind.go b/bind.go new file mode 100644 index 0000000..800fff8 --- /dev/null +++ b/bind.go @@ -0,0 +1,44 @@ +package strip + +import ( + "errors" + "reflect" +) + +func Bind(input interface{}) error { + + // Get the value of the input + v := reflect.ValueOf(input) + + // Ensure the input is a pointer + if v.Kind() == reflect.Ptr { + if v.IsNil() { + return errors.New("input is a nil pointer") + } + v = v.Elem() + } + + // Get the type of the value + t := v.Type() + + // Ensure the dereferenced value is a struct + if t.Kind() != reflect.Struct { + return errors.New("input is not a struct or pointer to a struct") + } + + // Iterate through the fields + for i := 0; i < v.NumField(); i++ { + field := t.Field(i) // Get the field + value := v.Field(i) // Get the field's value + tag := field.Tag.Get("strip_tag") // Get the 'validate' tag + + // Check if the tag is set to "true" and the field is a string + if tag == "true" && value.Kind() == reflect.String { + // Strip the HTML tags + clean := StripTags(value.String()) + value.SetString(clean) + } + } + + return nil +} diff --git a/bind_test.go b/bind_test.go new file mode 100644 index 0000000..91dc9eb --- /dev/null +++ b/bind_test.go @@ -0,0 +1,26 @@ +package strip + +import "testing" + +func TestBindStruct(t *testing.T) { + // Sample struct + type user struct { + Name string `strip_tag:"true"` + Age int + Email string `strip_tag:"true"` + Photo string + } + + users := user{ + Name: "John Doe", + Age: 30, + Email: "", + Photo: "