Skip to content

Commit

Permalink
Feat/update codebase analyzer v0.9.2 (#4)
Browse files Browse the repository at this point in the history
* feat: update codebase-analyzer library

* chore(GOMOD): Update go mod with new codebase analyzer version

* chore(SDK example): Add SDK V4 examples
  • Loading branch information
Chadiii authored Oct 22, 2024
1 parent 8cd098b commit 14c8ebc
Show file tree
Hide file tree
Showing 37 changed files with 268 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flagship/flagship.dart';

// Step 1 - Start the Flagship sdk with default configuration.
Flagship.start("_ENV_ID_", "_API_KEY_");

// Step 2 - Create visitor with context "isVip" : true
var visitor = Flagship.newVisitor(visitorId: "visitorId", hasConsented: true)
.withContext({"isVip": true}).build();

// Step 3 - Fetch flags
visitor.fetchFlags().whenComplete(() {
// Step 4 - Get Flag key
var flag = v.getFlag("displayVipFeature");
// Step 5 - Read Flag value
var value = flag.value(false);
var value1 = v.getFlag("backgroundColor").value("red");
var value1 = v.getFlag("backgroundSize").value(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package SDK_V2

func main() {
// Using the Decision API (default)
fsClient, _ := flagship.Start("environmentID", "apiKey")
fsClient, err := flagship.Start(environmentID, apiKey)

// Using the Bucketing mode
fsClient, _ = flagship.Start("environmentID", "apiKey", client.WithBucketing())
fsClient, err := flagship.Start(environmentID, apiKey, client.WithBucketing())

// Create visitor context
context := map[string]interface{}{
Expand All @@ -14,11 +14,11 @@ func main() {
"name": "visitor",
}
// Create a visitor
fsVisitor, _ := fsClient.NewVisitor("visitor_id", context)
fsVisitor, err := fsClient.NewVisitor("visitor_id", context)

// Update a single key
fsVisitor.UpdateContextKey("vipUser", true)
fsVisitor.UpdateContextKey("age", 30)
fsVisitor.UpdateContextLey("age", 30)

// Update the whole context
newContext := map[string]interface{}{
Expand All @@ -32,9 +32,6 @@ func main() {
discountName, err := fsVisitor.GetModificationNumber("btnSize", 13, true)
discountName, err := fsVisitor.GetModificationBool("showBtn", false, true)

// these flags will be analyzed using the custom-regex file example-regex.json
// try the command: flagship analyze flag list --custom-regex-json ./cmd/analyze/flag/example-regex.json --directory ./cmd/analyze/code-samples/abtasty-cli/go/
flagValue, _ := example.BoolVariation("my-boolean-flag", false)
flagValue1, _ := example.StringVariation("my-string-flag", "defaltVal")
flagValue2, _ := example.Float64Variation("my-numbers-flag", 13.6)
// If there is not error (and if there is, your value will still be set to defaut), you can use your modification value in your business logic
discountValue := getDiscountFromDB(discountName)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Flagship

// Step 1 - Start the Flagship sdk with default configuration.
Flagship.sharedInstance.start(envId: "_ENV_ID_", apiKey: "_API_KEY_")

// Step 2 - Create visitor with context "isVip" : true
let visitor = Flagship.sharedInstance.newVisitor(visitorId: "visitorId", hasConsented: true)
.withContext(context: ["isVip": true])
.build()

// Step 3 - Fetch flags
visitor.fetchFlags {

// Fetch completed

// Step 4 - Get Flag key
let flag = visitor.getFlag(key: "btnColor")

// Step 5 - Read Flag value
let value = flag.value(defaultValue: "red")

let value = visitor.getFlag(key: "displayVipFeature").value(defaultValue: false)

// Step 4 - Get Flag key
let flag2 = visitor.getFlag(key: "vipFeature")

// Step 5 - Read Flag value
let value = flag2.value(defaultValue: 16)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
val flagRank = visitor.getFlag("btnColor");
val flagRankValue = flagRank.value("red");

val flagRankValue2 = visitor.getFlag("backgroundSize").value(1);

val flagRank1 = visitor.getFlag("showBackground");
val flagRankValue = flagRank1.value(true);

val flagRank12 = visitor.getFlag("showBackground1");
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//start demo
// Usage: node demo/index.js
const express = require("express");
const { Flagship, HitType, EventCategory } = require("@flagship.io/js-sdk");

const app = express();
app.use(express.json());

const visitorId = "visitor-id";

// Step 1: Start the Flagship SDK by providing the environment ID and API key
Flagship.start("<ENV_ID>", "<API_KEY>", {
fetchNow: false,
});

// Endpoint to get an item
app.get("/item", async (req, res) => {

// Step 2: Create a new visitor with a visitor ID and consent status
const visitor = Flagship.newVisitor({
visitorId,
hasConsented: true,
context: {
fs_is_vip: true,
},
});

// Step 3: Fetch the flags for the visitor
await visitor.fetchFlags();

// fe:flag:fs_disable_coupon, string
const fsDisableCoupon = visitor.getFlag("fs_disable_coupon");

// Step 4: Get the values of the flags for the visitor
const fsEnableDiscount = visitor.getFlag("fs_enable_discount");
const fsAddToCartBtnColor = visitor.getFlag("fs_add_to_cart_btn_color");

const fsEnableDiscountValue = fsEnableDiscount.getValue(false);
const fsAddToCartBtnColorValue = fsAddToCartBtnColor.getValue("blue");

res.json({
item: {
name: "Flagship T-shirt",
price: 20,
},
fsEnableDiscount: fsEnableDiscountValue,
fsAddToCartBtnColor: fsAddToCartBtnColorValue,
});
});

// Endpoint to add an item to the cart
app.post("/add-to-cart", async (req, res) => {

const visitor = Flagship.newVisitor({
visitorId,
hasConsented: true
});

// Step 5: Send a hit to track an action
visitor.sendHit({
type: HitType.EVENT,
category: EventCategory.ACTION_TRACKING,
action: "add-to-cart-clicked",
});

res.json(null);
});

const port = 3000;

app.listen(port, () => {
console.log(`Server running on port ${port}`);
});

//end demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Flagship } from "@flagship.io/js-sdk";

Flagship.start("your_env_id", "your_api_key");

const visitor = Flagship.newVisitor({
visitorId: "your_visitor_id",
context: { isVip: true },
});

visitor.fetchFlags();

const variableKey = visitor.getFlag("flagKey");
const boxSizeFlagDefaultValue = variableKey.getValue("flagDefaultValue");

const variableKey1: any = visitor.getFlag("flagKey1");

const variableKey3: any = visitor.getFlag("flagKey3");
const boxSizeFlagDefaultValue1 = variableKey3.getValue(16);

const variableKey4: any = visitor.getFlag("flagKey4");

const variableKey5: any = visitor.getFlag("flagKey5").getValue(false);

// fe:flag: flagKey6, true
const variable6: any = visitor.getFlag("flagKey6");

visitor.getFlag("flagKey5").getValue(false);

visitor.getFlagValue("FlagKey5", false);
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Flagship.Main;

//Step 2: Create a visitor
var visitor = Fs.NewVisitor("<VISITOR_ID>", true)
.SetContext(new Dictionary<string, object>(){["isVip"] = true})
.Build();


//Step 3: Fetch flag from the Flagship platform
await visitor.FetchFlags();

/* Step 4: Retrieves a flag named "displayVipFeature",
*/
var flag = visitor.GetFlag("showBtn");

//Step 5: get the flag value and if the flag does not exist, it returns the default value "false"
var flagValue = flag.GetValue(false);

var flag_ = visitor.GetFlag("btnSize").GetValue(15);

var flag1 = visitor.GetFlag("btnColor");
var flagValue = flag1.GetValue("red");

Console.WriteLine($"Flag {flagValue}");
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
open System.Collections.Generic
open Flagship

let client = FlagshipBuilder.Start("ENV_ID","API_KEY");

let context = new Dictionary<string, obj>();
context.Add("key", "value");

let visitor = client.NewVisitor("visitor_id", context);

visitor.FetchFlags();

let btnColorFlag = visitor.GetFlag("btnColor");
let btnColorFlagValue = btnColorFlag.GetValue('red');

let flag = visitor.GetFlag("btnSize");
let flagValue = flag.GetValue(13);

let showBtnFlag = visitor.GetFlag("showBtn");
let showBtnFlagValue = showBtnFlag.GetValue(true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use Flagship\Flagship;

// Step 1: start the SDK
Flagship::start("<ENV_ID>", "<API_KEY>");

//Step 2: Create a visitor
$visitor = Flagship::newVisitor("<VISITOR_ID>", true)
->setContext(["isVip" => true])
->build();

//Step 3: Fetch flag from the Flagship platform
$visitor->fetchFlags();

//Step 4: Retrieves a flag named "displayVipFeature"
$flag = $visitor->getFlag("displayVipFeature");

//Step 5: Returns the flag value ,or if the flag does not exist, it returns the default value "false"
echo "flag value:". $flag->getValue(false);

$flag1 = $visitor->getFlag("vipFeatureSize")->getValue(15);

$flag1 = $visitor->getFlag("vipFeatureColor")->getValue("red");

//Step 6: Batch all the collected hits and send them
Flagship::close();
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {useFsFlag} from "@flagship.io/react-sdk";
export const MyReactComponent = () => {
const backgroundColorFlag = useFsFlag("backgroundColor", "green")
const btnColorFlag = useFsFlag("btnColor", "red")
const backgroundSize = useFsFlag("backgroundColor", 16)
const backgroundSize = useFsFlag("backgroundSize", 16)
const showBtn = useFsFlag("showBtn", true)

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";
import { useFsFlag } from "@flagship.io/react-sdk";

export const MyReactComponent = () => {
///Step 2: Retrieves a flag named "backgroundColor"
const flag = useFsFlag("backgroundColor")

//Step 3: Returns the value of the flag or if the flag does not exist, it returns the default value "green"
const flagValue = flag.getValue("green")


const flag_ = useFsFlag("btnSize").getValue(16)

const flag1 = useFsFlag("showBtn")
const flagValue_ = flag1.getValue(true)

return (
<button
style={{
height: "200px",
width: "200px",
backgroundColor: flagValue,
}}
>
{"I'm a square with color=" + flagValue}
</button>
);
};
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/d5/tengo/v2 v2.13.0
github.com/flagship-io/codebase-analyzer v0.8.0
github.com/flagship-io/codebase-analyzer v0.9.3
github.com/kyokomi/emoji/v2 v2.2.12
github.com/rodaine/table v1.1.0
github.com/sirupsen/logrus v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/flagship-io/codebase-analyzer v0.8.0 h1:/aTkBm3W2c1XGnwIde2DtrGYUJbIVYkm5+TXgQDmYro=
github.com/flagship-io/codebase-analyzer v0.8.0/go.mod h1:6+KRy0IPSkhxBv9CZf6Xjh+AfJFjJxWH6zGuT8a6x0c=
github.com/flagship-io/codebase-analyzer v0.9.3 h1:c7SQ8Zu5dO5e8JxymggTRTGGnCGaoQLbme1k2oGJbJI=
github.com/flagship-io/codebase-analyzer v0.9.3/go.mod h1:6+KRy0IPSkhxBv9CZf6Xjh+AfJFjJxWH6zGuT8a6x0c=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
Expand Down

0 comments on commit 14c8ebc

Please sign in to comment.