vchunikhin commented on code in PR #22556: URL: https://github.com/apache/beam/pull/22556#discussion_r955976273
########## learning/tour-of-beam/backend/cmd/ci_cd/ci_cd.go: ########## @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "context" + "fmt" + "log" + "os" + + "beam.apache.org/learning/tour-of-beam/backend/internal/fs_content" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + "cloud.google.com/go/datastore" +) + +var repo storage.Iface + +func init() { + client, err := datastore.NewClient(context.Background(), "") Review Comment: It seems that we can remove the second parameter here because the default value of string is an empty string ########## learning/tour-of-beam/backend/cmd/ci_cd/ci_cd.go: ########## @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "context" + "fmt" + "log" + "os" + + "beam.apache.org/learning/tour-of-beam/backend/internal/fs_content" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + "cloud.google.com/go/datastore" +) + +var repo storage.Iface + +func init() { + client, err := datastore.NewClient(context.Background(), "") + if err != nil { + log.Fatalf("new datastore client: %v", err) + } + repo = &storage.DatastoreDb{Client: client} +} + +func main() { + learningRoot := os.Getenv("TOB_LEARNING_ROOT") Review Comment: While you don't use viper lib i recommend you set "TOB_LEARNING_ROOT" as a constant here and anywhere in the project for environment variables at all ########## learning/tour-of-beam/backend/function.go: ########## @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package tob + +import ( + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "os" + + tob "beam.apache.org/learning/tour-of-beam/backend/internal" + "beam.apache.org/learning/tour-of-beam/backend/internal/service" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + + "cloud.google.com/go/datastore" + "github.com/GoogleCloudPlatform/functions-framework-go/functions" +) + +var svc service.IContent + +func init() { + // dependencies + // required: + // * TOB_MOCK: respond with static samples + // OR + // * DATASTORE_PROJECT_ID: cloud project id + // optional: + // * DATASTORE_EMULATOR_HOST: emulator host/port (ex. 0.0.0.0:8888) + if os.Getenv("TOB_MOCK") > "" { + svc = &service.Mock{} + } else { + client, err := datastore.NewClient(context.Background(), "") + if err != nil { + log.Fatalf("new datastore client: %v", err) + } + svc = &service.Svc{Repo: &storage.DatastoreDb{Client: client}} + } + + // functions framework + functions.HTTP("sdkList", sdkList) Review Comment: Maybe... can we develop the function that return the name of function and this function itself by reflection? I want to avoid the extra action required from a developer, namely, write function name always yee... i'm lazy, i know :) ########## learning/tour-of-beam/backend/internal/entity.go: ########## @@ -0,0 +1,69 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal + +type Unit struct { + Id string `json:"unitId"` + Name string `json:"name"` + + // optional + Description string `json:"description,omitempty"` + Hints []string `json:"hints,omitempty"` + TaskSnippetId string `json:"taskSnippetId,omitempty"` + SolutionSnippetId string `json:"solutionSnippetId,omitempty"` + TaskName string `json:"-"` + SolutionName string `json:"-"` + + // optional, user-specific + UserSnippetId string `json:"userSnippetId,omitempty"` + IsCompleted string `json:"is_completed,omitempty"` Review Comment: use camelCase everywhere, don't use "is_completed" here ########## learning/tour-of-beam/backend/cmd/ci_cd/ci_cd.go: ########## @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "context" + "fmt" + "log" + "os" + + "beam.apache.org/learning/tour-of-beam/backend/internal/fs_content" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + "cloud.google.com/go/datastore" +) + +var repo storage.Iface + +func init() { + client, err := datastore.NewClient(context.Background(), "") + if err != nil { + log.Fatalf("new datastore client: %v", err) + } + repo = &storage.DatastoreDb{Client: client} +} + +func main() { + learningRoot := os.Getenv("TOB_LEARNING_ROOT") + fmt.Printf("Parsing learning-content at %q\n", learningRoot) Review Comment: logs should be written with lower case ########## learning/tour-of-beam/backend/cmd/ci_cd/ci_cd.go: ########## @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "context" + "fmt" + "log" + "os" + + "beam.apache.org/learning/tour-of-beam/backend/internal/fs_content" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + "cloud.google.com/go/datastore" +) + +var repo storage.Iface + +func init() { + client, err := datastore.NewClient(context.Background(), "") + if err != nil { + log.Fatalf("new datastore client: %v", err) + } + repo = &storage.DatastoreDb{Client: client} Review Comment: Yes, we're able to code like that but i'd create a constructor for this structure, up to you ########## learning/tour-of-beam/backend/cmd/ci_cd/ci_cd.go: ########## @@ -0,0 +1,51 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "context" + "fmt" + "log" + "os" + + "beam.apache.org/learning/tour-of-beam/backend/internal/fs_content" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + "cloud.google.com/go/datastore" +) + +var repo storage.Iface + +func init() { + client, err := datastore.NewClient(context.Background(), "") + if err != nil { + log.Fatalf("new datastore client: %v", err) + } + repo = &storage.DatastoreDb{Client: client} +} + +func main() { + learningRoot := os.Getenv("TOB_LEARNING_ROOT") + fmt.Printf("Parsing learning-content at %q\n", learningRoot) + trees, err := fs_content.CollectLearningTree(learningRoot) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("collected %v sdks\n", len(trees)) + if err = repo.SaveContentTrees(context.Background(), trees); err != nil { Review Comment: It's the second point where you use context.Background() in this file. The best way to declare one context above and use him anywhere. Don't create extra contexts ########## learning/tour-of-beam/backend/function.go: ########## @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package tob + +import ( + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "os" + + tob "beam.apache.org/learning/tour-of-beam/backend/internal" + "beam.apache.org/learning/tour-of-beam/backend/internal/service" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + + "cloud.google.com/go/datastore" + "github.com/GoogleCloudPlatform/functions-framework-go/functions" +) + +var svc service.IContent + +func init() { + // dependencies + // required: + // * TOB_MOCK: respond with static samples + // OR + // * DATASTORE_PROJECT_ID: cloud project id + // optional: + // * DATASTORE_EMULATOR_HOST: emulator host/port (ex. 0.0.0.0:8888) + if os.Getenv("TOB_MOCK") > "" { Review Comment: Why did you use '>' here? ########## learning/tour-of-beam/backend/cmd/main.go: ########## @@ -0,0 +1,36 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "log" + "os" + + // Blank-import the function package so the init() runs + _ "beam.apache.org/learning/tour-of-beam/backend" + "github.com/GoogleCloudPlatform/functions-framework-go/funcframework" +) + +func main() { + // Use PORT environment variable, or default to 8080. + port := "8080" Review Comment: Should be as a constant in the file ########## learning/tour-of-beam/backend/function.go: ########## @@ -0,0 +1,93 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package tob + +import ( + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "os" + + tob "beam.apache.org/learning/tour-of-beam/backend/internal" + "beam.apache.org/learning/tour-of-beam/backend/internal/service" + "beam.apache.org/learning/tour-of-beam/backend/internal/storage" + + "cloud.google.com/go/datastore" + "github.com/GoogleCloudPlatform/functions-framework-go/functions" +) + +var svc service.IContent + +func init() { + // dependencies + // required: + // * TOB_MOCK: respond with static samples + // OR + // * DATASTORE_PROJECT_ID: cloud project id + // optional: + // * DATASTORE_EMULATOR_HOST: emulator host/port (ex. 0.0.0.0:8888) + if os.Getenv("TOB_MOCK") > "" { + svc = &service.Mock{} + } else { + client, err := datastore.NewClient(context.Background(), "") + if err != nil { + log.Fatalf("new datastore client: %v", err) + } + svc = &service.Svc{Repo: &storage.DatastoreDb{Client: client}} + } + + // functions framework + functions.HTTP("sdkList", sdkList) + functions.HTTP("getContentTree", getContentTree) + //functions.HTTP("getUnitContent", getUnitContent) +} + +func finalizeErrResponse(w http.ResponseWriter, status int, code, message string) { + w.WriteHeader(status) + resp := tob.CodeMessage{Code: code, Message: message} + _ = json.NewEncoder(w).Encode(resp) +} + +func sdkList(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, `{"names": ["Java", "Python", "Go"]}`) +} + +func getContentTree(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + if r.Method != "GET" { + w.WriteHeader(http.StatusMethodNotAllowed) + return + } + + sdkStr := r.URL.Query().Get("sdk") + sdk := tob.FromString(sdkStr) + if sdk == tob.SDK_UNDEFINED { + finalizeErrResponse(w, http.StatusBadRequest, "BAD_FORMAT", fmt.Sprintf("Bad sdk: %v", sdkStr)) Review Comment: use constants here too for these messages as "BAD_FORMAT" and "INTERNAL_ERROR" ########## learning/tour-of-beam/backend/go.mod: ########## @@ -0,0 +1,60 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +module beam.apache.org/learning/tour-of-beam/backend + +go 1.18 + +require ( + github.com/GoogleCloudPlatform/functions-framework-go v1.5.3 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + cloud.google.com/go v0.102.1 // indirect Review Comment: It looks that you should run "mod tidy" again because this command, as i know, groups dependencies into direct and indirect ########## learning/tour-of-beam/backend/internal/storage/adapter.go: ########## @@ -0,0 +1,146 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package storage + +import ( + "fmt" + + tob "beam.apache.org/learning/tour-of-beam/backend/internal" + "cloud.google.com/go/datastore" +) + +func sdk2Key(sdk tob.Sdk) string { Review Comment: Mmm.... maybe sdkToKey or just toKey will be better I just don't like use "2" instead of to :) ########## learning/tour-of-beam/backend/internal/entity.go: ########## @@ -0,0 +1,69 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package internal Review Comment: Are you sure that it's ok that the internal package associated with entities? Maybe should we create a model package or smth like that? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
