Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save a table schema descriptor to a file path #8

Closed
3 tasks done
georgeslabreche opened this issue Oct 9, 2017 · 5 comments
Closed
3 tasks done

Save a table schema descriptor to a file path #8

georgeslabreche opened this issue Oct 9, 2017 · 5 comments
Labels

Comments

@georgeslabreche
Copy link

georgeslabreche commented Oct 9, 2017

  • Schema: Change method name from "write" to "save"
  • Schema: Pretty print.
  • Table: Change method name from "write" to "save"
@georgeslabreche
Copy link
Author

georgeslabreche commented Oct 9, 2017

@pwalsh: This has already been implemented here:

public void write(String outputFilePath) throws IOException{
try (FileWriter file = new FileWriter(outputFilePath)) {
file.write(this.getJson().toString());
}
}

Test case is here:

@Test
public void testWrite() throws Exception{
File createdFile = folder.newFile("test_schema.json");
Schema createdSchema = new Schema();
Map<String, Object> intFieldConstraints = new HashMap();
intFieldConstraints.put(Field.CONSTRAINT_KEY_REQUIRED, true);
Field intField = new Field("id", Field.FIELD_TYPE_INTEGER, Field.FIELD_FORMAT_DEFAULT, null, null, intFieldConstraints);
createdSchema.addField(intField);
Map<String, Object> stringFieldConstraints = new HashMap();
stringFieldConstraints.put(Field.CONSTRAINT_KEY_MIN_LENGTH, 36);
stringFieldConstraints.put(Field.CONSTRAINT_KEY_MAX_LENGTH, 45);
Field stringField = new Field("name", Field.FIELD_TYPE_STRING, Field.FIELD_FORMAT_DEFAULT, "the title", "the description", stringFieldConstraints);
createdSchema.addField(stringField);
createdSchema.write(createdFile.getAbsolutePath());
Schema readSchema = new Schema(createdFile.getAbsolutePath());
Assert.assertEquals(readSchema.getField("id").getType(), Field.FIELD_TYPE_INTEGER);
Assert.assertEquals(readSchema.getField("id").getFormat(), Field.FIELD_FORMAT_DEFAULT);
Assert.assertEquals(readSchema.getField("id").getTitle(), "");
Assert.assertEquals(readSchema.getField("id").getDescription(), "");
Assert.assertTrue((boolean)readSchema.getField("id").getConstraints().get(Field.CONSTRAINT_KEY_REQUIRED));
Assert.assertEquals(readSchema.getField("name").getType(), Field.FIELD_TYPE_STRING);
Assert.assertEquals(readSchema.getField("name").getFormat(), Field.FIELD_FORMAT_DEFAULT);
Assert.assertEquals(readSchema.getField("name").getTitle(), "the title");
Assert.assertEquals(readSchema.getField("name").getDescription(), "the description");
Assert.assertEquals(36, readSchema.getField("name").getConstraints().get(Field.CONSTRAINT_KEY_MIN_LENGTH));
Assert.assertEquals(45, readSchema.getField("name").getConstraints().get(Field.CONSTRAINT_KEY_MAX_LENGTH));
}

Is it meant to be invokable from somewhere else?

@georgeslabreche
Copy link
Author

Lack of prettyprint is probably problematic...

@roll
Copy link
Member

roll commented Oct 10, 2017

@georgeslabreche
I've missed it because was searching for schema.save according to naming conventions - https://github.com/frictionlessdata/implementations#tableschema.

Could you rename it to save for consistency?

@georgeslabreche
Copy link
Author

georgeslabreche commented Oct 10, 2017

@roll you bet! What about prettyprint. Is it going to be a problem that it's not supported?

@roll
Copy link
Member

roll commented Oct 11, 2017

@georgeslabreche
It's better to preserve readability because a common use case is:

  • save descriptor to a file
  • update the file manually
  • load the file to continue the work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants