Просмотр исходного кода

init a procedure architecture designed & tested.

master
Patrick Peng Sun 8 лет назад
Родитель
Сommit
3bbfbd7509
3 измененных файлов: 121 добавлений и 0 удалений
  1. +63
    -0
      chatState.go
  2. +7
    -0
      chatState_test.go
  3. +51
    -0
      procGetBasicUserInfo.go

+ 63
- 0
chatState.go Просмотреть файл



import ( import (
"encoding/json" "encoding/json"
"errors"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
Hint string `json:"Hint"` Hint string `json:"Hint"`
Message map[string]string `json:"Message"` //the description for receiving message Message map[string]string `json:"Message"` //the description for receiving message
} `json:"Receive"` } `json:"Receive"`

Save map[string]string `json:"Save"` //the state save some data for later usage
} }


//for individual state //for individual state
err = os.Remove(path) err = os.Remove(path)
return return
} }

//ValidationResult After input validation, what is the result
type ValidationResult struct {
accept bool
Hint string
Error string
Warning string
}

//Validator function type for validating all wechat inputs
type Validator func(s chatState) ValidationResult

//start a procedure
func startProcedure(openID, procedure string) (err error) {
//init procedure state
init := getProcedureInit(openID, procedure)
if init == nil {
msg := "FATAL: cannot initialize procedure [" + procedure + "] "
err = errors.New(msg)
return
}
//init and get initial state
state := init(openID)

//do the real concret work for processing the state
err = processProcedureState(state)
return
}

//resume a previous Procedure
func resumeProcedure(procedure string) {

}

func stopProcedure(procedure string) {

}

func processProcedureState(state chatState) (err error) {
log.Println(state)
return
}

type initProcedureFunction func(openid string) (initState chatState)

func getProcedureInit(openID, procedure string) initProcedureFunction {
initFunc := map[string]initProcedureFunction{
"TestDummy": nil,
"TestEcho": initTestEcho,
"GetBasicUserInfo": initGetBasicUserInfo,
"GetEmailAddr": initGetBasicUserInfo,
}
return initFunc[procedure]
}

func initTestEcho(openid string) (r chatState) {
r.Name = openid
r.Expire = 0
return
}

+ 7
- 0
chatState_test.go Просмотреть файл

AssertEqual(t, err, nil, "delete chatState should be good") AssertEqual(t, err, nil, "delete chatState should be good")


} }

func TestStartProcedure(t *testing.T) {
s := startProcedure("id", "TestDummy")
AssertEqual(t, s != nil, true, "TestDummy should return error")
s = startProcedure("id", "TestEcho")
AssertEqual(t, s, nil, "TestEqual should return success")
}

+ 51
- 0
procGetBasicUserInfo.go Просмотреть файл

package main

//procedure 000
//available state
var statesGetBasicUserInfo = map[string]chatState{
"Intro": chatState{},
"AskName": chatState{},
"AskEnglishName": chatState{},
"AskEmail": chatState{},
"AskMobileNumber": chatState{},
"AskProfilePhoto": chatState{},
"Done": chatState{},
}

var validateGetBasicUserInfo = map[string]Validator{
"validateChineseName": validateChineseName,
}

func proc000AskName(openid string) {
s := chatState{}
s.Name = "AskName"
s.Expire = 300 //5 minutes
s.Save = map[string]string{} //clear
s.Send.Message["q"] = "请输入您的真实中文名,没有请填写 ”无“ "
s.Receive.Validator = "validateChineseName"
}

func validateChineseName(s chatState) (r ValidationResult) {
r.accept = true
r.Error = ""

input := s.Receive.Message["name"]
r.Hint = "通常中文名只有三个字或者四个字,比如 王更新,诸葛亮,司马相如,慕容白雪"
if len(input) >= 10 {
r.accept = false
r.Error = "您的名字过长"
}
if len(input) <= 1 {
r.accept = false
r.Error = "您的名字很萌,就是有点太短了"
}

if len(input) >= 5 {
r.Warning = "您的名字有点长,可以勉强接收"
}
return
}

func initGetBasicUserInfo(openid string) (initState chatState) {
return
}

Загрузка…
Отмена
Сохранить