Skip to content

Commit

Permalink
Fix code in online formatter
Browse files Browse the repository at this point in the history
Summary:
Online formatter was using an API that got updated, but we didn't reflect that and our test only caught when we actually did the bump :-/

We really need to setup things so that this does not happen anymore, like better testing

Reviewed By: davidtorosyan

Differential Revision: D58271711

fbshipit-source-id: 1d7acfc9093cb673e3765cd7ed5d7908e782d819
  • Loading branch information
Nivaldo Bondança authored and facebook-github-bot committed Jun 7, 2024
1 parent dfdb66a commit 8605080
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions online_formatter/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ package com.facebook.ktfmt.onlineformatter
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.RequestHandler
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
import com.facebook.ktfmt.cli.ParseResult
import com.facebook.ktfmt.cli.ParsedArgs
import com.facebook.ktfmt.format.Formatter
import com.google.gson.Gson
import java.io.ByteArrayOutputStream
import java.io.PrintStream

class Handler : RequestHandler<APIGatewayProxyRequestEvent, String> {
init {
Expand All @@ -35,16 +34,24 @@ class Handler : RequestHandler<APIGatewayProxyRequestEvent, String> {
return gson.toJson(
try {
val request = gson.fromJson(event.body, Request::class.java)
val parsingErrors = ByteArrayOutputStream()
val style = request.style
val parsedArgs =
ParsedArgs.parseOptions(
PrintStream(parsingErrors), if (style == null) arrayOf() else arrayOf(style))
Response(
Formatter.format(
parsedArgs.formattingOptions.copy(maxWidth = request.maxWidth ?: 100),
request.source ?: ""),
parsingErrors.toString().ifEmpty { null })
val parseResult = ParsedArgs.parseOptions(listOfNotNull(style).toTypedArray())
when (parseResult) {
is ParseResult.Ok -> {
val parsedArgs = parseResult.parsedValue
Response(
source =
Formatter.format(
parsedArgs.formattingOptions.copy(maxWidth = request.maxWidth ?: 100),
request.source.orEmpty(),
),
err = null,
)
}
is ParseResult.Error -> {
Response(null, parseResult.errorMessage)
}
}
} catch (e: Exception) {
Response(null, e.message)
})
Expand Down

0 comments on commit 8605080

Please sign in to comment.