Skip to content

Commit

Permalink
feat: adds KindInt to Attribute Kinds and updates README.md with new …
Browse files Browse the repository at this point in the history
…attribute query instructions

Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
  • Loading branch information
jpower432 committed Aug 16, 2022
1 parent 8b6bbaf commit 246a53d
Show file tree
Hide file tree
Showing 36 changed files with 573 additions and 182 deletions.
91 changes: 51 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ uor-client-go version
2. Use the `uor-client-go build` command to build the workspace as an OCI artifact in build-cache The default location is ~/.uor/cache. It an be set with the `UOR_CACHE` environment variable`.
3. Use the `uor-client-go push` command to publish to a registry as an OCI artifact.
4. Use the `uor-client-go pull` command to pull the artifact back to a local workspace.
5. Use the `uor-client-go inspect` command to inspect the build to list information about references.
5. 5. Use the `uor-client-go inspect` command to inspect the build cache to list information about references.

### Build workspace into an artifact

Expand Down Expand Up @@ -85,58 +85,69 @@ uor-client-go pull localhost:5000/myartifacts:latest -o my-output-directory --at
2. Add the content to be uploaded in the directory (can be files of any content types).
3. Create a json doc where the value of each kv pair is the path to each file within the directory. Multiple json docs can be used to create deep graphs, but a graph must only have one root. Multiple json docs in a build directory is for advanced use cases. Most use cases do not need more than one json doc.

Example json doc:

```
{
"fish": "fish.jpg",
"text": "subdir1/file.txt",
"fish2": "subdir1/fish2.jpg"
}
```
Example json doc:
```
{
"fish": "fish.jpg",
"text": "subdir1/file.txt",
"fish2": "subdir1/fish2.jpg"
}
```

4. Create a dataset-config.yaml outside of the content directory that references the relative paths from within the content directory to each file. Add user defined key value pairs as subkeys to the `annotations`section. Each file should have as many attributes as possible. Multiple files can be referenced by using the `*` wildcard.

Example dataset-config.yaml:

```
kind: DataSetConfiguration
apiVersion: client.uor-framework.io/v1alpha1
files:
- file: fish.jpg
attributes:
animal: fish
habitat: ocean
size: small
color: blue
- file: subdir1/file.txt
attributes:
fiction: true
genre: science fiction
- file: *.jpg
attributes:
custom: customval
```
Example dataset-config.yaml:

```
kind: DataSetConfiguration
apiVersion: client.uor-framework.io/v1alpha1
collection:
files:
- file: "fish.jpg"
attributes:
animal: "fish"
habitat: "ocean"
size: "small"
color: "blue"
- file: "subdir1/file.txt"
attributes:
fiction: true
genre: "science fiction"
- file: "*.jpg"
attributes:
custom: "customval"
```

5. Run the UOR client build command referencing the dataset config, the content directory, and the destination registry location.
```
uor-client-go build my-workspace localhost:5000/test/dataset:latest --dsconfig dataset-config.yaml
```
```
uor-client-go build my-workspace localhost:5000/test/dataset:latest --dsconfig dataset-config.yaml
```
6. Run the UOR push command to publish
```
uor-client-go push localhost:5000/test/dataset:latest
```
```
uor-client-go push localhost:5000/test/dataset:latest
```

7. Optionally inspect the OCI manifest of the dataset:
`curl -H "Accept: application/vnd.oci.image.manifest.v1+json" <servername>:<port>/v2/<namespace>/<repo>/manifests/<digest or tag>`

8. Optionally inspect the cache:
`uor-client-go inspect`
`uor-client-go inspect`

9. Optionally pull the collection back down to verify the content with `uor-client-go pull`:
`uor-client-go pull localhost:5000/test/dataset:latest -o my-output-directory`
`uor-client-go pull localhost:5000/test/dataset:latest -o my-output-directory`

10. Optionally pull a subset of the collection back down to verify the content with `uor-client-go pull`:
`uor-client-go pull localhost:5000/test/dataset:latest -o my-output-directory --attributes "fiction=true"`

Example attribute-query.yaml:
```
kind: AttributeQuery
apiVersion: client.uor-framework.io/v1alpha1
attributes:
fiction: true
```
`uor-client-go pull localhost:5000/test/dataset:latest -o my-output-directory --attributes attribute-query.yaml`

# Glossary

Expand Down
18 changes: 14 additions & 4 deletions attributes/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,26 @@ func (a Attributes) Exists(input model.Attribute) (bool, error) {
return false, err
}
return outS == inS, nil
case model.KindNumber:
outN, err := val.AsNumber()
case model.KindFloat:
outF, err := val.AsFloat()
if err != nil {
return false, err
}
inN, err := input.AsNumber()
inF, err := input.AsFloat()
if err != nil {
return false, err
}
return outN == inN, nil
return outF == inF, nil
case model.KindInt:
outI, err := val.AsInt()
if err != nil {
return false, err
}
inI, err := input.AsInt()
if err != nil {
return false, err
}
return outI == inI, nil
case model.KindBool:
outB, err := val.AsBool()
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions attributes/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ func TestAttributes_AsJSON(t *testing.T) {
expString := `{"name":"test","size":2}`
test := Attributes{
"name": NewString("name", "test"),
"size": NewNumber("size", 2),
"size": NewInt("size", 2),
}
require.Equal(t, expString, string(test.AsJSON()))
}

func TestAttributes_Exists(t *testing.T) {
test := Attributes{
"name": NewString("name", "test"),
"size": NewNumber("size", 2),
"size": NewInt("size", 2),
}
exists, err := test.Exists(NewString("name", "test"))
require.NoError(t, err)
require.True(t, exists)
exists, err = test.Exists(NewNumber("size", 2))
exists, err = test.Exists(NewInt("size", 2))
require.NoError(t, err)
require.True(t, exists)
}

func TestAttributes_Find(t *testing.T) {
test := Attributes{
"name": NewString("name", "test"),
"size": NewNumber("size", 2),
"size": NewInt("size", 2),
}
val := test.Find("name")
require.Equal(t, "name", val.Key())
Expand All @@ -44,15 +44,15 @@ func TestAttributes_Find(t *testing.T) {
func TestAttributes_Len(t *testing.T) {
test := Attributes{
"name": NewString("name", "test"),
"size": NewNumber("size", 2),
"size": NewInt("size", 2),
}
require.Equal(t, 2, test.Len())
}

func TestAttributes_List(t *testing.T) {
test := Attributes{
"name": NewString("name", "test"),
"size": NewNumber("size", 2),
"size": NewInt("size", 2),
}
list := test.List()
require.Len(t, list, 2)
Expand Down
10 changes: 8 additions & 2 deletions attributes/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ func (a boolAttribute) AsString() (string, error) {
return "", ErrWrongKind
}

// AsNumber returns the value as a number value errors if that is not
// AsFloat returns the value as a number value errors if that is not
// the underlying type.
func (a boolAttribute) AsNumber() (float64, error) {
func (a boolAttribute) AsFloat() (float64, error) {
return 0, ErrWrongKind
}

// AsInt returns the value as an int value errors and if that is not
// the underlying type.
func (a boolAttribute) AsInt() (int, error) {
return 0, ErrWrongKind
}

Expand Down
16 changes: 14 additions & 2 deletions attributes/bool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,28 @@ func TestBoolAttribute_AsBool(t *testing.T) {
require.Equal(t, true, b)
}

func TestBoolAttribute_AsNumber(t *testing.T) {
func TestBoolAttribute_AsFloat(t *testing.T) {
test := NewBool("test", false)
b, err := test.AsNumber()
b, err := test.AsFloat()
require.ErrorIs(t, ErrWrongKind, err)
require.Equal(t, float64(0), b)
}

func TestBoolAttribute_AsInt(t *testing.T) {
test := NewBool("test", false)
b, err := test.AsInt()
require.ErrorIs(t, ErrWrongKind, err)
require.Equal(t, 0, b)
}

func TestBoolAttribute_AsString(t *testing.T) {
test := NewBool("test", false)
b, err := test.AsString()
require.ErrorIs(t, ErrWrongKind, err)
require.Equal(t, "", b)
}

func TestBoolAttribute_IsNull(t *testing.T) {
test := NewBool("test", false)
require.False(t, test.IsNull())
}
59 changes: 59 additions & 0 deletions attributes/float.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package attributes

import "github.com/uor-framework/uor-client-go/model"

type floatAttribute struct {
key string
value float64
}

var _ model.Attribute = floatAttribute{}

// NewFloat returns a number attribute.
func NewFloat(key string, value float64) model.Attribute {
return floatAttribute{key: key, value: value}
}

// Kind returns the kind for the attribute.
func (a floatAttribute) Kind() model.Kind {
return model.KindFloat
}

// Key return the attribute key.
func (a floatAttribute) Key() string {
return a.key
}

// IsNull returns whether the value is null.
func (a floatAttribute) IsNull() bool {
return false
}

// AsBool returns the value as a boolean errors if that is not
// the underlying type.
func (a floatAttribute) AsBool() (bool, error) {
return false, ErrWrongKind
}

// AsString returns the value as a string errors if that is not
// the underlying type.
func (a floatAttribute) AsString() (string, error) {
return "", ErrWrongKind
}

// AsFloat returns the value as a number value and errors if that is not
// the underlying type.
func (a floatAttribute) AsFloat() (float64, error) {
return a.value, nil
}

// AsInt returns the value as a number value and errors if that is not
// the underlying type.
func (a floatAttribute) AsInt() (int, error) {
return 0, ErrWrongKind
}

// AsAny returns the value as an interface.
func (a floatAttribute) AsAny() interface{} {
return a.value
}
45 changes: 45 additions & 0 deletions attributes/float_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package attributes

import (
"github.com/stretchr/testify/require"
"github.com/uor-framework/uor-client-go/model"
"testing"
)

func TestFloatAttribute_Kind(t *testing.T) {
test := NewFloat("test", 1)
require.Equal(t, model.KindFloat, test.Kind())
}

func TestFloatAttribute_AsBool(t *testing.T) {
test := NewFloat("test", 1)
n, err := test.AsBool()
require.ErrorIs(t, ErrWrongKind, err)
require.Equal(t, false, n)
}

func TestFloatAttribute_AsFloat(t *testing.T) {
test := NewFloat("test", 1)
n, err := test.AsFloat()
require.NoError(t, err)
require.Equal(t, float64(1), n)
}

func TestFloatAttribute_AsInt(t *testing.T) {
test := NewFloat("test", 1)
n, err := test.AsInt()
require.ErrorIs(t, ErrWrongKind, err)
require.Equal(t, 0, n)
}

func TestFloatAttribute_AsString(t *testing.T) {
test := NewFloat("test", 1)
n, err := test.AsString()
require.ErrorIs(t, ErrWrongKind, err)
require.Equal(t, "", n)
}

func TestFloatAttribute_IsNull(t *testing.T) {
test := NewFloat("test", 1.0)
require.False(t, test.IsNull())
}
Loading

0 comments on commit 246a53d

Please sign in to comment.