Giter VIP home page Giter VIP logo

Comments (1)

oklahomer avatar oklahomer commented on May 17, 2024

This change may be ideal when we purely design an interface that represents user input. As a matter of fact, all inputs do not necessarily represent text messages; some represent images while some represent video clips. However, go-sarah has a handy feature to supply a regular expression to be run against user inputs.

go-sarah/command.go

Lines 296 to 306 in a7408dc

// MatchPattern is a setter to provide command match pattern.
// This regular expression is used to find matching command with given Input.
//
// Use MatchFunc to set more customizable matching logic.
func (builder *CommandPropsBuilder) MatchPattern(pattern *regexp.Regexp) *CommandPropsBuilder {
builder.props.matchFunc = func(input Input) bool {
// https://golang.org/doc/go1.6#minor_library_changes
return pattern.Copy().MatchString(input.Message())
}
return builder
}

If the design is to focus on rigorousness, there could be an idea to define an additional interface that represents a text format message, sarah.TextInput. The above method may check if the given sarah.Input implements sarah.TextInput and then run the current logic.
The question is if such rigorousness is preferred. Such changes unintentionally lead to more complexity and cumbersomeness since all sarah.Command implementation will have to check if the given input implements sarah.TextInput to do some common tasks. A command implementation can be somewhat like below:

type command struct {
}

func (*command) Execute(ctx context.Context, input sarah.Input) (*sarah.CommandResponse, error) {
	textInput, ok := input.(sarah.TextInput)
	if !ok {
		return nil, nil
	}

	message := textInput.Message()

	// TODO Do something with message

	return nil, nil
}

func (*command) Match(sarah.Input) bool {
	textInput, ok := input.(sarah.TextInput)
	if !ok {
		return false
	}

	return strings.HasPrefix(textInput.Message(), ".dummyPrefix")
}

func (*command) Identifier() string {
	panic("example")
}

func (*command) Instruction(input *sarah.HelpInput) string {
	panic(`input ".dummyPrefix xxx" to do some good`)
}

This is too complex comparing to current implementation where Input.Message() is always available with sarah.Input. Let the current interface stay. The current interface definition actually guides to return an empty message when the input does not represent a text message.

go-sarah/input.go

Lines 19 to 22 in a7408dc

// Message returns the text form of user input.
// This may return empty string when this Input implementation represents non-text payload such as photo,
// video clip or file.
Message() string

from go-sarah.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.