gagliardetto / anchor-go

Generate Go clients from anchor IDLs for Solana blockchain programs
MIT License
138 stars 31 forks source link

Hacky fix #3

Closed evulse closed 2 years ago

evulse commented 2 years ago

If a method ends in a number "doSomething2" then the AnchorTypeIDEncoding is wrong as it treats it as do_something_2 instead of the required do_something2

I only needed to generate once so I didn't really solve this correctly as it's across multiple repos

gagliardetto commented 2 years ago

Hi @evulse

Thanks for the PR! I retrieved the tests from the letter case converter libraries used in anchor, and the behaviour is more nuanced than I expected.

Here's a branch with your code + tests : https://github.com/gagliardetto/anchor-go/blob/sighash/sighash_test.go

gagliardetto commented 2 years ago

See here the non-passing tests:

https://github.com/gagliardetto/anchor-go/runs/4685675257?check_suite_focus=true#step:4:24983

gagliardetto commented 2 years ago
--- FAIL: TestToSnakeForSighash (0.01s)
    --- FAIL: TestToSnakeForSighash/typescript (0.00s)
        --- FAIL: TestToSnakeForSighash/typescript/_id (0.00s)
            sighash_test.go:31: 
                    Error Trace:    sighash_test.go:31
                    Error:          Not equal: 
                                    expected: "id"
                                    actual  : "_id"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -id
                                    +_id
                    Test:           TestToSnakeForSighash/typescript/_id
    --- FAIL: TestToSnakeForSighash/rust (0.01s)
        --- FAIL: TestToSnakeForSighash/rust/This_is_Human_case. (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "this_is_human_case"
                                    actual  : "this_is_human_case_"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -this_is_human_case
                                    +this_is_human_case_
                    Test:           TestToSnakeForSighash/rust/This_is_Human_case.
        --- FAIL: TestToSnakeForSighash/rust/MixedUP_CamelCase,_with_some_Spaces (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "mixed_up_camel_case_with_some_spaces"
                                    actual  : "mixed_up_camel_case,_with_some_spaces"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -mixed_up_camel_case_with_some_spaces
                                    +mixed_up_camel_case,_with_some_spaces
                    Test:           TestToSnakeForSighash/rust/MixedUP_CamelCase,_with_some_Spaces
        --- FAIL: TestToSnakeForSighash/rust/mixed_up__snake_case_with_some__spaces (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "mixed_up_snake_case_with_some_spaces"
                                    actual  : "mixed_up__snake_case_with_some__spaces"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -mixed_up_snake_case_with_some_spaces
                                    +mixed_up__snake_case_with_some__spaces
                    Test:           TestToSnakeForSighash/rust/mixed_up__snake_case_with_some__spaces
        --- FAIL: TestToSnakeForSighash/rust/this-contains__ALLKinds_OfWord_Boundaries (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "this_contains_all_kinds_of_word_boundaries"
                                    actual  : "this_contains__all_kinds_of_word_boundaries"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -this_contains_all_kinds_of_word_boundaries
                                    +this_contains__all_kinds_of_word_boundaries
                    Test:           TestToSnakeForSighash/rust/this-contains__ALLKinds_OfWord_Boundaries
        --- FAIL: TestToSnakeForSighash/rust/XΣXΣ_baffle (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "xσxς_baffle"
                                    actual  : "xΣxΣ_baffle"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -xσxς_baffle
                                    +xΣxΣ_baffle
                    Test:           TestToSnakeForSighash/rust/XΣXΣ_baffle
        --- FAIL: TestToSnakeForSighash/rust/99BOTTLES (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "99bottles"
                                    actual  : "99_bottles"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -99bottles
                                    +99_bottles
                    Test:           TestToSnakeForSighash/rust/99BOTTLES
        --- FAIL: TestToSnakeForSighash/rust/abc123def456 (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "abc123def456"
                                    actual  : "abc123_def456"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -abc123def456
                                    +abc123_def456
                    Test:           TestToSnakeForSighash/rust/abc123def456
        --- FAIL: TestToSnakeForSighash/rust/ABC123def456 (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "abc123def456"
                                    actual  : "abc123_def456"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -abc123def456
                                    +abc123_def456
                    Test:           TestToSnakeForSighash/rust/ABC123def456
        --- FAIL: TestToSnakeForSighash/rust/ABC123DEF456 (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "abc123def456"
                                    actual  : "abc123_def456"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -abc123def456
                                    +abc123_def456
                    Test:           TestToSnakeForSighash/rust/ABC123DEF456
        --- FAIL: TestToSnakeForSighash/rust/ABC123DEf456 (0.01s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "abc123d_ef456"
                                    actual  : "abc123_d_ef456"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -abc123d_ef456
                                    +abc123_d_ef456
                    Test:           TestToSnakeForSighash/rust/ABC123DEf456
        --- FAIL: TestToSnakeForSighash/rust/ABC123dEEf456FOO (0.00s)
            sighash_test.go:74: 
                    Error Trace:    sighash_test.go:74
                    Error:          Not equal: 
                                    expected: "abc123d_e_ef456_foo"
                                    actual  : "abc123_d_e_ef456_foo"

                                    Diff:
                                    --- Expected
                                    +++ Actual
                                    @@ -1 +1 @@
                                    -abc123d_e_ef456_foo
                                    +abc123_d_e_ef456_foo
                    Test:           TestToSnakeForSighash/rust/ABC123dEEf456FOO
FAIL
FAIL    github.com/gagliardetto/anchor-go   0.354s
gagliardetto commented 2 years ago

I'm starting to doubt that the typescript and rust snake case libraries used in anchor actually produce the same results (they most likely have edge cases that don't match),

gagliardetto commented 2 years ago

I'm porting the snake case func to match the implementation and results of the rust library used in anchor.

gagliardetto commented 2 years ago

Fixed.

You can try it out with go get github.com/gagliardetto/anchor-go@2d027e0df2579b69cf0b42b2b844995152a565ff

Will merge to main and make a release tomorrow.

See https://github.com/gagliardetto/anchor-go/pull/5

gagliardetto commented 2 years ago

Closing in favor of https://github.com/gagliardetto/anchor-go/pull/5