You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by "keon94 (via GitHub)" <gi...@apache.org> on 2023/05/25 19:17:45 UTC

[GitHub] [incubator-devlake] keon94 commented on a diff in pull request #5286: 5250 define migration script on python side

keon94 commented on code in PR #5286:
URL: https://github.com/apache/incubator-devlake/pull/5286#discussion_r1205917974


##########
backend/core/utils/json.go:
##########
@@ -0,0 +1,67 @@
+/*
+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 utils
+
+import (
+	"fmt"
+	"reflect"
+
+	"github.com/apache/incubator-devlake/core/errors"
+)
+
+type JsonObject = map[string]any
+type JsonArray = []any
+
+func GetProperty[T any](object JsonObject, key string) (T, errors.Error) {
+	property, ok := object[key]
+	if !ok {
+		return *new(T), errors.Default.New(fmt.Sprintf("Missing property %s", key))
+	}
+	return convert[T](property)
+}
+
+func GetItem[T any](array JsonArray, index int) (T, errors.Error) {
+	if index < 0 || index >= len(array) {
+		return *new(T), errors.Default.New(fmt.Sprintf("Index %d out of range", index))
+	}
+	return convert[T](array[index])
+}
+
+func convert[T any](value any) (T, errors.Error) {

Review Comment:
   I think this function can be useful outside of this file. I suggest you export it (e.g. call it Convert) and add a few unit tests for it.



-- 
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: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org