Skip to content

Commit

Permalink
✨ : add HclParser class
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Nov 15, 2019
1 parent 2f7b5bc commit 6c2b03c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 33 deletions.
40 changes: 40 additions & 0 deletions src/main/java/io/codeka/gaia/hcl/HclParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.codeka.gaia.hcl

import io.codeka.gaia.hcl.antlr.hclLexer
import io.codeka.gaia.hcl.antlr.hclParser
import io.codeka.gaia.modules.bo.Output
import io.codeka.gaia.modules.bo.Variable
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream

class HclParser {

private fun parseContent(content: String): HclVisitor {
// loading test file
val charStream = CharStreams.fromString(content)

// configuring antlr lexer
val lexer = hclLexer(charStream)

// using the lexer to configure a token stream
val tokenStream = CommonTokenStream(lexer)

// configuring antlr parser using the token stream
val parser = hclParser(tokenStream)

// visit the AST
val hclVisitor = HclVisitor()
hclVisitor.visit(parser.file())
return hclVisitor
}

fun parseVariables(content:String): List<Variable> {
val hclVisitor = parseContent(content)
return hclVisitor.variables
}

fun parseOutputs(content:String): List<Output> {
val hclVisitor = parseContent(content)
return hclVisitor.outputs
}
}
58 changes: 25 additions & 33 deletions src/test/java/io/codeka/gaia/hcl/HCLParserTest.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
package io.codeka.gaia.hcl;

import io.codeka.gaia.hcl.antlr.hclLexer;
import io.codeka.gaia.hcl.antlr.hclParser;
import io.codeka.gaia.modules.bo.Output;
import io.codeka.gaia.modules.bo.Variable;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.charset.Charset;

import static org.assertj.core.api.Assertions.assertThat;

class HCLParserTest {

private HclVisitor visitFile(String fileName) throws IOException {
// loading test file
CharStream charStream = CharStreams.fromStream(new ClassPathResource(fileName).getInputStream());

// configuring antlr lexer
hclLexer lexer = new hclLexer(charStream);

// configuring antlr parser
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
hclParser parser = new hclParser(tokenStream);

// when
// visit the AST
var hclVisitor = new HclVisitor();
hclVisitor.visit(parser.file());

return hclVisitor;
}
private HclParser hclParser = new HclParser();

@Test
void parsing_variables_shouldWorkWithVisitor() throws IOException {
// given
var hclVisitor = this.visitFile("hcl/variables.tf");
var fileContent = IOUtils.toString(new ClassPathResource("hcl/variables.tf").getURL(), Charset.defaultCharset());

// when
var variables = hclParser.parseVariables(fileContent);

// then
assertThat(hclVisitor.getVariables()).hasSize(3);
assertThat(variables).hasSize(3);

var stringVar = new Variable("\"string_var\"");
stringVar.setType("\"string\"");
Expand All @@ -56,39 +39,48 @@ void parsing_variables_shouldWorkWithVisitor() throws IOException {
var boolVar = new Variable("\"bool_var\"");
boolVar.setDefaultValue("false");

assertThat(hclVisitor.getVariables()).contains(stringVar, numberVar, boolVar);
assertThat(variables).contains(stringVar, numberVar, boolVar);
}

@Test
void parsing_variables_shouldWork_withComplexFile() throws IOException {
// given
var hclVisitor = this.visitFile("hcl/variables_aws_eks.tf");
var fileContent = IOUtils.toString(new ClassPathResource("hcl/variables_aws_eks.tf").getURL(), Charset.defaultCharset());

// when
var variables = hclParser.parseVariables(fileContent);

// then
assertThat(hclVisitor.getVariables()).hasSize(49);
assertThat(variables).hasSize(49);
}

@Test
void parsing_outputs_shouldWorkWithVisitor() throws IOException {
// given
var hclVisitor = this.visitFile("hcl/outputs.tf");
var fileContent = IOUtils.toString(new ClassPathResource("hcl/outputs.tf").getURL(), Charset.defaultCharset());

// when
var outputs = hclParser.parseOutputs(fileContent);

// then
assertThat(hclVisitor.getOutputs()).hasSize(2);
assertThat(outputs).hasSize(2);

var output1 = new Output("\"instance_ip_addr\"", "\"${aws_instance.server.private_ip}\"", "\"The private IP address of the main server instance.\"", "false");
var output2 = new Output("\"db_password\"", "aws_db_instance.db[1].password", "\"The password for logging in to the database.\"", "true");

assertThat(hclVisitor.getOutputs()).contains(output1, output2);
assertThat(outputs).contains(output1, output2);
}

@Test
void parsing_outputs_shouldWork_withComplexFile() throws IOException {
// given
var hclVisitor = this.visitFile("hcl/outputs_aws_eks.tf");
var fileContent = IOUtils.toString(new ClassPathResource("hcl/outputs_aws_eks.tf").getURL(), Charset.defaultCharset());

// when
var outputs = hclParser.parseOutputs(fileContent);

// then
assertThat(hclVisitor.getOutputs()).hasSize(27);
assertThat(outputs).hasSize(27);
}

}

0 comments on commit 6c2b03c

Please sign in to comment.