跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

reflectutil.go

64 行 · 1.8 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4// This file has been adapted from https://github.com/containerd/nerdctl/blob/v1.0.0/pkg/reflectutil/reflectutil.go
5/*
6 Copyright The containerd Authors.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16*/
17
18package reflectutil
19
20import (
21 "fmt"
22 "reflect"
23)
24
25func UnknownNonEmptyFields(structOrStructPtr any, knownNames ...string) []string {
26 var unknown []string
27 knownNamesMap := make(map[string]struct{}, len(knownNames))
28 for _, name := range knownNames {
29 knownNamesMap[name] = struct{}{}
30 }
31 origVal := reflect.ValueOf(structOrStructPtr)
32 var val reflect.Value
33 switch origVal.Kind() {
34 case reflect.Pointer:
35 val = origVal.Elem()
36 case reflect.Struct:
37 val = origVal
38 default:
39 panic(fmt.Errorf("expected pointer or struct, got %v", origVal.Kind()))
40 }
41 for i := range val.NumField() {
42 iField := val.Field(i)
43 if isEmpty(iField) {
44 continue
45 }
46 iName := val.Type().Field(i).Name
47 if _, ok := knownNamesMap[iName]; !ok {
48 unknown = append(unknown, iName)
49 }
50 }
51 return unknown
52}
53
54func isEmpty(v reflect.Value) bool {
55 // NOTE: IsZero returns false for zero-length map and slice
56 if v.IsZero() {
57 return true
58 }
59 switch v.Kind() {
60 case reflect.Map, reflect.Slice:
61 return v.Len() == 0
62 }
63 return false
64}