Skip to content

Commit 6b07589

Browse files
authored
[Feature] Extend Shared Types (#1911)
1 parent d894656 commit 6b07589

File tree

6 files changed

+191
-2
lines changed

6 files changed

+191
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- (Feature) (Platform) OpenID Logout Endpoints
2323
- (Maintenance) Grade Doc Field
2424
- (Feature) (Platform) Improve Platform Components names
25+
- (Feature) Extend Shared Types
2526

2627
## [1.2.48](https://github.com/arangodb/kube-arangodb/tree/1.2.48) (2025-05-08)
2728
- (Maintenance) Extend Documentation

pkg/apis/shared/v1/any.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2025 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package v1
22+
23+
import (
24+
"encoding/json"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util"
27+
"github.com/arangodb/kube-arangodb/pkg/util/errors"
28+
)
29+
30+
var _ json.Marshaler = &Any{}
31+
var _ json.Unmarshaler = &Any{}
32+
33+
type Any []byte
34+
35+
func NewAny[T any](in T) (Any, error) {
36+
return json.Marshal(in)
37+
}
38+
39+
func FromAny[T any](in Any) (T, error) {
40+
var z T
41+
if err := json.Unmarshal(in, &z); err != nil {
42+
return z, err
43+
}
44+
45+
return z, nil
46+
}
47+
48+
func (d Any) MarshalJSON() ([]byte, error) {
49+
ret := make([]byte, len(d))
50+
copy(ret, d)
51+
return ret, nil
52+
}
53+
54+
func (d Any) SHA256() string {
55+
return util.SHA256(d)
56+
}
57+
58+
func (d *Any) UnmarshalJSON(bytes []byte) error {
59+
if d == nil {
60+
return errors.Errorf("nil object provided")
61+
}
62+
63+
ret := make([]byte, len(bytes))
64+
copy(ret, bytes)
65+
66+
*d = ret
67+
68+
return nil
69+
}

pkg/apis/shared/v1/any_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2025 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package v1
22+
23+
import (
24+
"encoding/json"
25+
"reflect"
26+
"testing"
27+
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
func testAnyRepresentation[IN any](t *testing.T, v IN) {
32+
t.Run(reflect.TypeFor[IN]().String(), func(t *testing.T) {
33+
d, err := NewAny[IN](v)
34+
require.NoError(t, err)
35+
36+
z, err := json.Marshal(d)
37+
require.NoError(t, err)
38+
39+
t.Logf("%s", string(z))
40+
41+
var d2 Any
42+
43+
require.NoError(t, json.Unmarshal(z, &d2))
44+
45+
require.EqualValues(t, d, d2)
46+
})
47+
}
48+
49+
func Test_Any_Representation(t *testing.T) {
50+
testAnyRepresentation(t, "some_Test_Any")
51+
testAnyRepresentation(t, []string{"A", "B"})
52+
testAnyRepresentation(t, 53)
53+
testAnyRepresentation(t, Object{
54+
Name: "X",
55+
})
56+
}

pkg/apis/shared/v1/data.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2024-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -31,6 +31,20 @@ import (
3131
var _ json.Marshaler = &Data{}
3232
var _ json.Unmarshaler = &Data{}
3333

34+
func NewData[T any](in T) (Data, error) {
35+
return json.Marshal(in)
36+
}
37+
38+
func FromData[T any](in Data) (T, error) {
39+
var z T
40+
if err := json.Unmarshal(in, &z); err != nil {
41+
return z, err
42+
}
43+
44+
return z, nil
45+
}
46+
47+
// Data keeps the representation of the object in the base64 encoded string
3448
type Data []byte
3549

3650
func (d Data) MarshalJSON() ([]byte, error) {

pkg/apis/shared/v1/data_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2024 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2024-2025 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121
package v1
2222

2323
import (
24+
"encoding/json"
25+
"reflect"
2426
"testing"
2527

2628
"github.com/stretchr/testify/require"
@@ -35,3 +37,30 @@ func Test_Data(t *testing.T) {
3537
require.NoError(t, err)
3638
require.EqualValues(t, d, z)
3739
}
40+
41+
func testDataRepresentation[IN any](t *testing.T, v IN) {
42+
t.Run(reflect.TypeFor[IN]().String(), func(t *testing.T) {
43+
d, err := NewData[IN](v)
44+
require.NoError(t, err)
45+
46+
z, err := json.Marshal(d)
47+
require.NoError(t, err)
48+
49+
t.Logf("%s", string(z))
50+
51+
var d2 Data
52+
53+
require.NoError(t, json.Unmarshal(z, &d2))
54+
55+
require.EqualValues(t, d, d2)
56+
})
57+
}
58+
59+
func Test_Data_Representation(t *testing.T) {
60+
testDataRepresentation(t, "some_Test_data")
61+
testDataRepresentation(t, []string{"A", "B"})
62+
testDataRepresentation(t, 53)
63+
testDataRepresentation(t, Object{
64+
Name: "X",
65+
})
66+
}

pkg/apis/shared/v1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)