From b66891a646ac8878a87e899103489d9d00247df1 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 13:10:44 +0300 Subject: [PATCH 1/6] use latest commit from go-vise 0.2.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 0ab8488..151bfc2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd go 1.22.6 require ( - git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f + git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea github.com/alecthomas/assert/v2 v2.2.2 github.com/peteole/testdata-loader v0.3.0 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 diff --git a/go.sum b/go.sum index 1c00969..02dc10d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f h1:CuJvG3NyMoRtHUim4aZdrfjjJBg2AId7z0yp7Q97bRM= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea h1:6ZYT+dIjd/f5vn9y5AJDZ7SQQckA6w5ZfUoKygyI11o= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= From e6e701a609c1fa166ec6eb511748723c39eb2137 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 13:13:20 +0300 Subject: [PATCH 2/6] remove init and use Exec --- cmd/main.go | 2 +- internal/handlers/base.go | 14 +------------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 7176bd3..c03a8ef 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -96,7 +96,7 @@ func main() { en = en.WithDebug(nil) } - _, err = en.Init(ctx) + _, err = en.Exec(ctx, []byte{}) if err != nil { fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) os.Exit(1) diff --git a/internal/handlers/base.go b/internal/handlers/base.go index 9be5872..babd9fb 100644 --- a/internal/handlers/base.go +++ b/internal/handlers/base.go @@ -71,19 +71,7 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) } rqs.Engine = en - r, err = rqs.Engine.Init(rqs.Ctx) - if err != nil { - perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage) - rqs.Storage = nil - if perr != nil { - logg.ErrorCtxf(rqs.Ctx, "", "storage put error", perr) - } - return rqs, err - } - - if r && len(rqs.Input) > 0 { - r, err = rqs.Engine.Exec(rqs.Ctx, rqs.Input) - } + r, err = rqs.Engine.Exec(rqs.Ctx, rqs.Input) if err != nil { perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage) rqs.Storage = nil From a1056cf2877a90e16f79b7747be9d4e01f566bb4 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 13:54:23 +0300 Subject: [PATCH 3/6] removed unused code --- internal/handlers/single.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/handlers/single.go b/internal/handlers/single.go index ab10363..6929617 100644 --- a/internal/handlers/single.go +++ b/internal/handlers/single.go @@ -37,8 +37,6 @@ type RequestSession struct { Continue bool } -type engineMaker func(cfg engine.Config, rs resource.Resource, pr *persist.Persister) engine.Engine - // TODO: seems like can remove this. type RequestParser interface { GetSessionId(rq any) (string, error) From 9925b3a2e5373c760caacb1e793a417295023fc8 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 22:12:52 +0300 Subject: [PATCH 4/6] use updated latest commit from go-vise 0.2.0 --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 151bfc2..7cceaa6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd go 1.22.6 require ( - git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea + git.defalsify.org/vise.git v0.1.0-rc.3.0.20240920144308-b2d2c5f18f38 github.com/alecthomas/assert/v2 v2.2.2 github.com/peteole/testdata-loader v0.3.0 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 diff --git a/go.sum b/go.sum index 02dc10d..5d235cc 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f h1:CuJvG3Ny git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea h1:6ZYT+dIjd/f5vn9y5AJDZ7SQQckA6w5ZfUoKygyI11o= git.defalsify.org/vise.git v0.1.0-rc.3.0.20240914163514-577f56f43bea/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240920144308-b2d2c5f18f38 h1:4aAZijIcq33ixnZ+U48ckDIkwSfZL3St/CqoXZcC5K8= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240920144308-b2d2c5f18f38/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= From 63c7c65a00ba525ce49ffaa83cd3ca207bfc4c50 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 22:15:24 +0300 Subject: [PATCH 5/6] rename all WriteResult to Flush --- internal/handlers/base.go | 2 +- internal/http/at_session_handler.go | 2 +- internal/http/http_test.go | 10 +++++----- internal/mocks/httpmocks/enginemock.go | 12 ++++++------ 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/handlers/base.go b/internal/handlers/base.go index babd9fb..aa83e0b 100644 --- a/internal/handlers/base.go +++ b/internal/handlers/base.go @@ -87,7 +87,7 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) func(f *BaseSessionHandler) Output(rqs RequestSession) (RequestSession, error) { var err error - _, err = rqs.Engine.WriteResult(rqs.Ctx, rqs.Writer) + _, err = rqs.Engine.Flush(rqs.Ctx, rqs.Writer) return rqs, err } diff --git a/internal/http/at_session_handler.go b/internal/http/at_session_handler.go index 53c4ba2..25da954 100644 --- a/internal/http/at_session_handler.go +++ b/internal/http/at_session_handler.go @@ -87,6 +87,6 @@ func (ash *ATSessionHandler) Output(rqs handlers.RequestSession) (handlers.Reque return rqs, err } - _, err = rqs.Engine.WriteResult(rqs.Ctx, rqs.Writer) + _, err = rqs.Engine.Flush(rqs.Ctx, rqs.Writer) return rqs, err } \ No newline at end of file diff --git a/internal/http/http_test.go b/internal/http/http_test.go index 48d04ca..8f6f312 100644 --- a/internal/http/http_test.go +++ b/internal/http/http_test.go @@ -69,7 +69,7 @@ func TestATSessionHandler_ServeHTTP(t *testing.T) { mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp } mh.OutputFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil } mh.ResetFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil } - me.WriteResultFunc = func(context.Context, io.Writer) (int, error) { return 0, nil } + me.FlushFunc = func(context.Context, io.Writer) (int, error) { return 0, nil } }, formData: url.Values{ "phoneNumber": []string{"+1234567890"}, @@ -178,7 +178,7 @@ func TestATSessionHandler_Output(t *testing.T) { input: handlers.RequestSession{ Continue: true, Engine: &httpmocks.MockEngine{ - WriteResultFunc: func(context.Context, io.Writer) (int, error) { + FlushFunc: func(context.Context, io.Writer) (int, error) { return 0, nil }, }, @@ -192,7 +192,7 @@ func TestATSessionHandler_Output(t *testing.T) { input: handlers.RequestSession{ Continue: false, Engine: &httpmocks.MockEngine{ - WriteResultFunc: func(context.Context, io.Writer) (int, error) { + FlushFunc: func(context.Context, io.Writer) (int, error) { return 0, nil }, }, @@ -202,11 +202,11 @@ func TestATSessionHandler_Output(t *testing.T) { expectedError: false, }, { - name: "WriteResult error", + name: "Flush error", input: handlers.RequestSession{ Continue: true, Engine: &httpmocks.MockEngine{ - WriteResultFunc: func(context.Context, io.Writer) (int, error) { + FlushFunc: func(context.Context, io.Writer) (int, error) { return 0, errors.New("write error") }, }, diff --git a/internal/mocks/httpmocks/enginemock.go b/internal/mocks/httpmocks/enginemock.go index d5c6b20..12d07f4 100644 --- a/internal/mocks/httpmocks/enginemock.go +++ b/internal/mocks/httpmocks/enginemock.go @@ -7,10 +7,10 @@ import ( // MockEngine implements the engine.Engine interface for testing type MockEngine struct { - InitFunc func(context.Context) (bool, error) - ExecFunc func(context.Context, []byte) (bool, error) - WriteResultFunc func(context.Context, io.Writer) (int, error) - FinishFunc func() error + InitFunc func(context.Context) (bool, error) + ExecFunc func(context.Context, []byte) (bool, error) + FlushFunc func(context.Context, io.Writer) (int, error) + FinishFunc func() error } func (m *MockEngine) Init(ctx context.Context) (bool, error) { @@ -21,8 +21,8 @@ func (m *MockEngine) Exec(ctx context.Context, input []byte) (bool, error) { return m.ExecFunc(ctx, input) } -func (m *MockEngine) WriteResult(ctx context.Context, w io.Writer) (int, error) { - return m.WriteResultFunc(ctx, w) +func (m *MockEngine) Flush(ctx context.Context, w io.Writer) (int, error) { + return m.FlushFunc(ctx, w) } func (m *MockEngine) Finish() error { From e754773bce9488b7fd33feb91f4b2b4fc1ff8392 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Sep 2024 22:16:43 +0300 Subject: [PATCH 6/6] remove need for initial Exec before loop --- cmd/main.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index c03a8ef..26ffe3a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -96,13 +96,7 @@ func main() { en = en.WithDebug(nil) } - _, err = en.Exec(ctx, []byte{}) - if err != nil { - fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) - os.Exit(1) - } - - err = engine.Loop(ctx, en, os.Stdin, os.Stdout) + err = engine.Loop(ctx, en, os.Stdin, os.Stdout, nil) if err != nil { fmt.Fprintf(os.Stderr, "loop exited with error: %v\n", err) os.Exit(1)