Skip to content

Migrate to current Roc syntax#13

Merged
agu-z merged 2 commits into
agu-z:mainfrom
niclas-ahden:current-roc-syntax
Jun 17, 2025
Merged

Migrate to current Roc syntax#13
agu-z merged 2 commits into
agu-z:mainfrom
niclas-ahden:current-roc-syntax

Conversation

@niclas-ahden

@niclas-ahden niclas-ahden commented Apr 19, 2025

Copy link
Copy Markdown
Contributor
  • No backpassing
  • New record builders
  • PNC
  • snake_case

The API changes from:

    |> Pg.Cmd.expectN (
        Pg.Result.succeed {
            name: <- Pg.Result.str "name" |> Pg.Result.apply,
            price: <- Pg.Result.dec "price" |> Pg.Result.apply
        }
    )

To:

|> Pg.Cmd.expect_n(
    { Pg.Result.record_builder <-
        name: Pg.Result.str("name"),
        price: Pg.Result.dec("price"),
    },
)

Note Pg.Result.record_builder above. Pg.Cmd.succeed can still be used to return non-records like so:

|> Pg.Cmd.expect_n(
    Pg.Result.succeed(
        |name|
            |age|
                age_str = Num.to_str(age)
                "${name}: ${age_str}",
    )
    |> Pg.Result.with(Pg.Result.str("name"))
    |> Pg.Result.with(Pg.Result.u8("age")),
)

It's unclear to me whether this is desirable. However, given that the name Pg.Result.succeed is taken, I opted for
Pg.Result.record_builder for the records to make it discoverable and understandable.

This PR also includes the changes from #10 and #11.

This is a partial migration as I ran out of steam part-way through sql-cli and examples/store. I stopped when faced with the query builder and its use of the old record builders. There were no brain cells left over to start pondering that design. Hopefully this offering can still be helpful as a stepping stone toward current Roc!

- No backpassing
- New record builders
- PNC
- snake_case

The API changes from:

```roc
    |> Pg.Cmd.expectN (
        Pg.Result.succeed {
            name: <- Pg.Result.str "name" |> Pg.Result.apply,
            price: <- Pg.Result.dec "price" |> Pg.Result.apply
        }
    )
```
To:
```roc
|> Pg.Cmd.expect_n(
    { Pg.Result.record_builder <-
        name: Pg.Result.str("name"),
        price: Pg.Result.dec("price"),
    },
)
```

Note `Pg.Result.record_builder` above. `Pg.Cmd.succeed` can still be
used to return non-records like so:

```roc
|> Pg.Cmd.expect_n(
    Pg.Result.succeed(
        |name|
            |age|
                age_str = Num.to_str(age)
                "${name}: ${age_str}",
    )
    |> Pg.Result.with(Pg.Result.str("name"))
    |> Pg.Result.with(Pg.Result.u8("age")),
)
```

It's unclear to me whether this is desirable. However, given that the
name `Pg.Result.succeed` is taken, I opted for
`Pg.Result.record_builder` for the records to make it discoverable and
understandable.

This is a partial migration as I ran out of steam part-way through
`sql-cli` and `examples/store`.
niclas-ahden added a commit to growthagent/roc-pg that referenced this pull request Apr 19, 2025
@agu-z agu-z merged commit e01ef5b into agu-z:main Jun 17, 2025
@agu-z

agu-z commented Jun 17, 2025

Copy link
Copy Markdown
Owner

@niclas-ahden Thank you so much for all the effort you put into this. I'm going to clean up a few other things and make a release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants