-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat(server): move export csv items and GeoJSON to usecase layer #1329
Open
jasonkarel
wants to merge
11
commits into
main
Choose a base branch
from
chore-server/items-as-csv-to-usecase
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
833b541
feat(server): move export csv items to usecase layer
jasonkarel f27d71e
Merge branch 'main' into chore-server/items-as-csv-to-usecase
jasonkarel 0119c17
tidy code organization
jasonkarel 583ef20
add operator handling in ItemsAsCSV
jasonkarel 35c1dc0
move ItemsAsGeoJSON, ItemsWithProjectAsCSV, and ItemsWithProjectAsGeo…
jasonkarel 06101c7
add buildeSchemaPackage func in Item
jasonkarel e66d212
added UT for public methods
jasonkarel 744091d
Merge branch 'main' into chore-server/items-as-csv-to-usecase
jasonkarel c0876b1
Fix UT database save
jasonkarel ffc4052
fix race condition in project ut
jasonkarel fd90086
Merge branch 'main' into chore-server/items-as-csv-to-usecase
jasonkarel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package interactor | ||
|
||
import ( | ||
"encoding/csv" | ||
"io" | ||
|
||
"github.com/labstack/gommon/log" | ||
"github.com/reearth/reearth-cms/server/pkg/integrationapi" | ||
"github.com/reearth/reearth-cms/server/pkg/item" | ||
"github.com/reearth/reearth-cms/server/pkg/schema" | ||
"github.com/reearth/reearthx/i18n" | ||
"github.com/reearth/reearthx/rerror" | ||
"github.com/samber/lo" | ||
) | ||
|
||
var ( | ||
pointFieldIsNotSupportedError = rerror.NewE(i18n.T("point type is not supported in any geometry field in this model")) | ||
) | ||
|
||
// CSV | ||
func csvFromItems(pw *io.PipeWriter, l item.VersionedList, s *schema.Schema) error { | ||
if !s.IsPointFieldSupported() { | ||
return pointFieldIsNotSupportedError | ||
} | ||
|
||
go handleCSVGeneration(pw, l, s) | ||
|
||
return nil | ||
} | ||
|
||
func handleCSVGeneration(pw *io.PipeWriter, l item.VersionedList, s *schema.Schema) { | ||
err := generateCSV(pw, l, s) | ||
if err != nil { | ||
log.Errorf("failed to generate CSV: %v", err) | ||
_ = pw.CloseWithError(err) | ||
} else { | ||
_ = pw.Close() | ||
} | ||
} | ||
|
||
func generateCSV(pw *io.PipeWriter, l item.VersionedList, s *schema.Schema) error { | ||
w := csv.NewWriter(pw) | ||
defer w.Flush() | ||
|
||
headers := integrationapi.BuildCSVHeaders(s) | ||
if err := w.Write(headers); err != nil { | ||
return err | ||
} | ||
|
||
nonGeoFields := lo.Filter(s.Fields(), func(f *schema.Field, _ int) bool { | ||
return !f.IsGeometryField() | ||
}) | ||
|
||
for _, ver := range l { | ||
row, ok := integrationapi.RowFromItem(ver.Value(), nonGeoFields) | ||
if ok { | ||
if err := w.Write(row); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return w.Error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package interactor | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/reearth/reearth-cms/server/pkg/id" | ||
"github.com/reearth/reearth-cms/server/pkg/item" | ||
"github.com/reearth/reearth-cms/server/pkg/schema" | ||
"github.com/reearth/reearth-cms/server/pkg/value" | ||
"github.com/reearth/reearth-cms/server/pkg/version" | ||
"github.com/reearth/reearthx/account/accountdomain" | ||
"github.com/reearth/reearthx/util" | ||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCSVFromItems(t *testing.T) { | ||
iid := id.NewItemID() | ||
sid := id.NewSchemaID() | ||
mid := id.NewModelID() | ||
tid := id.NewThreadID() | ||
pid := id.NewProjectID() | ||
gst := schema.GeometryObjectSupportedTypeList{schema.GeometryObjectSupportedTypePoint, schema.GeometryObjectSupportedTypeLineString} | ||
gest := schema.GeometryEditorSupportedTypeList{schema.GeometryEditorSupportedTypePoint, schema.GeometryEditorSupportedTypeLineString} | ||
sf1 := schema.NewField(schema.NewGeometryObject(gst).TypeProperty()).NewID().Name("geo1").Key(id.RandomKey()).MustBuild() | ||
sf3 := schema.NewField(schema.NewGeometryEditor(gest).TypeProperty()).NewID().Name("geo2").Key(id.RandomKey()).MustBuild() | ||
in4, _ := schema.NewInteger(lo.ToPtr(int64(1)), lo.ToPtr(int64(100))) | ||
tp4 := in4.TypeProperty() | ||
sf4 := schema.NewField(tp4).NewID().Name("age").Key(id.RandomKey()).MustBuild() | ||
sf5 := schema.NewField(schema.NewBool().TypeProperty()).NewID().Name("isMarried").Key(id.RandomKey()).MustBuild() | ||
s1 := schema.New().ID(sid).Fields([]*schema.Field{sf1, sf3, sf4, sf5}).Workspace(accountdomain.NewWorkspaceID()).Project(pid).MustBuild() | ||
fi1 := item.NewField(sf1.ID(), value.TypeGeometryObject.Value("{\"coordinates\":[139.28179282584915,36.58570985749664],\"type\":\"Point\"}").AsMultiple(), nil) | ||
fi2 := item.NewField(sf3.ID(), value.TypeGeometryEditor.Value("{\"coordinates\":[139.28179282584915,36.58570985749664],\"type\":\"Point\"}").AsMultiple(), nil) | ||
fi3 := item.NewField(sf4.ID(), value.TypeInteger.Value(30).AsMultiple(), nil) | ||
fi4 := item.NewField(sf5.ID(), value.TypeBool.Value(true).AsMultiple(), nil) | ||
i1 := item.New(). | ||
ID(iid). | ||
Schema(sid). | ||
Project(pid). | ||
Fields([]*item.Field{fi1, fi2, fi3, fi4}). | ||
Model(mid). | ||
Thread(tid). | ||
MustBuild() | ||
v1 := version.New() | ||
vi1 := version.MustBeValue(v1, nil, version.NewRefs(version.Latest), util.Now(), i1) | ||
|
||
// with geometry fields | ||
ver1 := item.VersionedList{vi1} | ||
_, pw := io.Pipe() | ||
err := csvFromItems(pw, ver1, s1) | ||
assert.Nil(t, err) | ||
|
||
jasonkarel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// no geometry fields | ||
iid2 := id.NewItemID() | ||
sid2 := id.NewSchemaID() | ||
mid2 := id.NewModelID() | ||
tid2 := id.NewThreadID() | ||
sf2 := schema.NewField(schema.NewText(lo.ToPtr(10)).TypeProperty()).NewID().Key(id.RandomKey()).MustBuild() | ||
s2 := schema.New().ID(sid).Fields([]*schema.Field{sf2}).Workspace(accountdomain.NewWorkspaceID()).Project(pid).MustBuild() | ||
i2 := item.New(). | ||
ID(iid2). | ||
Schema(sid2). | ||
Project(pid). | ||
Fields([]*item.Field{item.NewField(sf2.ID(), value.TypeText.Value("test").AsMultiple(), nil)}). | ||
Model(mid2). | ||
Thread(tid2). | ||
MustBuild() | ||
v2 := version.New() | ||
vi2 := version.MustBeValue(v2, nil, version.NewRefs(version.Latest), util.Now(), i2) | ||
ver2 := item.VersionedList{vi2} | ||
expectErr2 := pointFieldIsNotSupportedError | ||
_, pw1 := io.Pipe() | ||
err = csvFromItems(pw1, ver2, s2) | ||
assert.Equal(t, expectErr2, err) | ||
|
||
// point field is not supported | ||
iid3 := id.NewItemID() | ||
sid3 := id.NewSchemaID() | ||
mid3 := id.NewModelID() | ||
tid3 := id.NewThreadID() | ||
gst2 := schema.GeometryObjectSupportedTypeList{schema.GeometryObjectSupportedTypeLineString, schema.GeometryObjectSupportedTypePolygon} | ||
sf6 := schema.NewField(schema.NewGeometryObject(gst2).TypeProperty()).NewID().Name("geo3").Key(id.RandomKey()).MustBuild() | ||
s3 := schema.New().ID(sid).Fields([]*schema.Field{sf6}).Workspace(accountdomain.NewWorkspaceID()).Project(pid).MustBuild() | ||
i3 := item.New(). | ||
ID(iid3). | ||
Schema(sid3). | ||
Project(pid). | ||
Fields([]*item.Field{item.NewField(sf6.ID(), value.TypeText.Value("{\n \"coordinates\": [\n [\n 139.65439725962517,\n 36.34793305387103\n ],\n [\n 139.61688622815393,\n 35.910803456352724\n ]\n ],\n \"type\": \"LineString\"\n}").AsMultiple(), nil)}). | ||
Model(mid3). | ||
Thread(tid3). | ||
MustBuild() | ||
v3 := version.New() | ||
vi3 := version.MustBeValue(v3, nil, version.NewRefs(version.Latest), util.Now(), i3) | ||
ver3 := item.VersionedList{vi3} | ||
expectErr3 := pointFieldIsNotSupportedError | ||
_, pw2 := io.Pipe() | ||
err = csvFromItems(pw2, ver3, s3) | ||
assert.Equal(t, expectErr3, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me!
Just a quick note: we have two functions that handle exporting items as CSV. You've already completed
ItemsAsCSV
, so please apply the same approach to theItemsWithProjectAsCSV
function. Once that's done, kindly remove thecsvFromItems
function from the adapter level.