Git Product home page Git Product logo

Comments (7)

srpn avatar srpn commented on May 29, 2024 1

Thanks for the response @fax4ever,
Yeah, the main question is how to add annotations to Animal and its concrete subclasses. As I mentioned, the above example with @AutoProtoSchemaBuilder fails to compile saying the @protofield type "should not be abstract".

abstract class Animal {
  String type;
}

class Dog extends Animal {
  String name;
  void bark();
  // ...
}

from protostream.

fax4ever avatar fax4ever commented on May 29, 2024 1

Yes! One-of seems to be the proper way to map the polymorphism.
At the moment I think that we don't support the schema generation for this case with @AutoProtoSchemaBuilder.
I'm opening an issue for it to the ProtoStream Jira => https://issues.redhat.com/projects/IPROTO.
In the meantime I think you could generate the mashallers and the proto schema manually, as we used to do.

from protostream.

fax4ever avatar fax4ever commented on May 29, 2024

Hi @srpn,
Exactly the idea of the ProtoAdapter is to generate the marshallers and the schema for class we cannot modify.
I think that you need to set the number attribute in the @ProtoField annotation.

from protostream.

fax4ever avatar fax4ever commented on May 29, 2024

Moreover, you should annotate the class Animal with the @ProtoFields or define a ProtoAdapter for it in turn.
Can you share more about Animal please?

from protostream.

fax4ever avatar fax4ever commented on May 29, 2024

What would be the Protocol Buffers schema you would like to generate for this case?

from protostream.

srpn avatar srpn commented on May 29, 2024

I am looking to use Infinispan for a distributed cache and I am new to Protocol Buffers.
From what I read, oneOf could be a way to represent this as a protobuf message

Here is a full example for ease of reproducing
error: org.infinispan.protostream.annotations.ProtoSchemaBuilderException: The type com.test.Animal of field 'animal' of com.test.CachedAnimalAdapter should not be abstract.
Code used:

package com.test;

import org.infinispan.protostream.annotations.ProtoAdapter;
import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;

@ProtoAdapter(CachedAnimal.class)
public class CachedAnimalAdapter {
    @ProtoFactory
    CachedAnimal create(Animal animal, String cacheDuration) {
        return new CachedAnimal(animal, cacheDuration);
    }

    @ProtoField(number = 1)
    Animal getAnimal(CachedAnimal cachedAnimal) {
        return cachedAnimal.getAnimal();
    }

    @ProtoField(number = 2)
    String getCacheDuration(CachedAnimal cachedAnimal) {
        return cachedAnimal.getCachedTime();
    }
}

// Let's say the below classes are from a 3rd party library that I cannot annotate
class CachedAnimal {
    private Animal animal;
    private String cachedTime;

    public CachedAnimal(Animal animal, String cachedTime) {
        this.animal = animal;
        this.cachedTime = cachedTime;
    }

    public Animal getAnimal() {
        return animal;
    }

    public String getCachedTime() {
        return cachedTime;
    }
}

abstract class Animal {
    String type;

    public Animal(String type) {
        this.type = type;
    }
}

class Dog extends Animal {
    private String breed;

    public Dog(String type, String breed) {
        super(type);
        this.breed = breed;
    }

    public void makeSound() {
        System.out.println("woof");
    }
}

class Cat extends Animal {
    private String name;

    public Cat(String type, String name) {
        super(type);
        this.name = name;
    }

    public void makeSound() {
        System.out.println("meow");
    }
}

from protostream.

fax4ever avatar fax4ever commented on May 29, 2024

created https://issues.redhat.com/browse/IPROTO-267

from protostream.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.